开课20天,学习的内容杂,在根本无法熟练使用的所学内容之下,完成了该功能单一、界面难看的目前较为能看的程序。该程序的实际思想简单,由页面,跳转传值,监听事件组成。
思路解析:
一、主要页面布局:主页面、内容列表页(继承ListActivity)、内容详情页、内容编辑页、头像选择页(GridView布局管理器)
完成主要功能:增、删、改、查。
1.增加功能的页面跳转实现:
主页面→内容编辑页(空)→点击“添加”(添加成功)
2.删除功能的页面跳转实现:
主页面→内容列表页→弹出警告框(删除成功)
3.修改功能的页面跳转实现:
主页面→内容列表页→内容编辑页(非空)→点击“修改”按钮(修改成功)
4.查询功能的页面跳转实现:
主页面→内容列表页→内容详情页
发现其中很多页面可以重复使用,就像是去学校有很多种方式一样,但单纯的页面跳转会有冲突,所以会给他加上特定标签(Intent传值),标明他是从哪来的,要干什么。
二、代码思路解析
20天积累的全部知识点在本程序中均有体现。
1.页面布局:
1-1.主页面等没有规律可言的页面,可用LinearLayout(线性布局)和RelativeLayout(相对布局).
1-2.内容列表页:源代码可继承ListActivity,xml文件写明每一行或一列所用的布局,类似于QQ列表
1-3.头像选择页:xml文件可GridView(网格布局)来进行布局
2.Activity的跳转的多种体现:
2-1.Bundle
实例化Intent:Intent intent = new Intent(this.目标Activity的class);
实例化Bundle:Bundle bundle = new Bundle();
加入数值的方法:
.putString("key","值");
.putInt("key",数值);
.putBundle("key",bundle);
将bundle绑在intent上
intent.putExtra(bundle;
通过startActivity(intent)方法进行传值
2-2.Intent
也可运用Intent直接传值,不用Budle。
实例化Intent:Intent intent = new Intent();
加入数值的方法:
.putString("key","值");
.putInt("key",数值);
.putBundle("key",bundle);
通过startActivity(intent)直接传值
2-3.Activity跳转,返回数据、结果
大体和戴值跳转相同,只是传值方法改为startActivityForResult(Intent,int);前者是数据,后者是请求码。
将第二个Activity用.setResult(int,Intent)传值.finish()方法清除,第一个Activity用onActivityResult(int,int,Intent){
super.onActivityResult(int,int,Intent);
}
3.列表页的形成
新建xml文件存放显示单行的容器和样式
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//构造器传入本页面对象和用于显示的数据
this.setListAdapter(new MyAdapter(this,MainActivity.lists));
}
class MyAdapter extends BaseAdapter{
private List<Person> lists;
LayoutInflater inflater;
public MyAdapter(Context context,List<Person> lists){
this.lists = lists;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lists.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return lists.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
/*
*页面优化
*优点:不是创建多个对象,而是只创建一个对象
*速度明显提升
*缺点:必须保证每个列表样式的要一模一样
*/
YouHua youhua = new YouHua();
if(convertView==null){
convertView = inflater.inflate(R.layout.item_select,null);
youhua.imageview = (ImageView)convertView.findViewById(R.id.imageview);
youhua.username = (TextView)convertView.findViewById(R.id.username);
youhua.address = (TextView)convertView.findViewById(R.id.address);
convertView.setTag(youhua);
}
YouHua yh = (YouHua) convertView.getTag();
// Log.i("tips",lists.get(position).getUsername());
yh.imageview.setImageResource(lists.get(position).getIcon());
yh.username.setText(lists.get(position).getUsername());
yh.address.setText(lists.get(position).getAddress());
return convertView;
}
class YouHua{
public ImageView imageview;
TextView username,context,address;
}
}
}
4.警告框的创建
AlertDialog.Builder builder = new AlertDialog.Builder(SelectActivity.this);
builder.setTitle("删除");
builder.setCancelable(true);
builder.setIcon(android.R.drawable.ic_delete);//设置图标
builder.setCancelable(false);//是否可以通过返回键退出对话框
builder.setMessage("真的要删除吗?");//设置消息
builder.setPositiveButton("确定",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int whith){
MainActivity.lists.remove(position);
SelectActivity.this.finish();
}
});
builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(SelectActivity.this,"取消",Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
本文记录了作者在学习Android的20天内,如何利用学到的知识实现一个简单学生管理系统的全过程。系统涵盖了增删改查功能,涉及页面布局、Activity跳转、数据传递以及警告框的创建等技术。主要页面包括主页面、列表页、详情页和编辑页,使用LinearLayout、RelativeLayout和GridView布局。通过Intent和Bundle进行页面间的数据传递,同时在ListView中实现了列表项的动态加载。

6958

被折叠的 条评论
为什么被折叠?



