一周总结11.11.13

实习的第一周,希望自己以后也能像这一周这样不动摇.

本周主要自己做了个学生体质健康数据管理系统的一些东西.那些简单的布局在这就不回顾了.

首先,数据的收集,关系就是年段,班级,跟学生的1:n的关系.所以建三个对象,年段对象中包含name,List<Clazz> clazzes(以班级对象而成的链表);班级对象包含name,List<Student> students(以学生对象而成的链表),学生对象则包含name,id,sex.以对象做链表主要是能在年段中包含班级,班级中包含学生.也为Intent的传递做铺垫.

其实,关于开机界面

setContentView(R.layout.loading);
//新建handler对象新建进程延时
new Handler().postDelayed(new Runnable(){

public void run() {
Intent intent = new Intent(LoadingActivity.this,MenuActivity.class);
startActivity(intent);
LoadingActivity.this.finish();
}
}, 2500);

网上还有一些以图片为开机,或者别的方法来实现.

关于ListView,首先给在一个布局中找到listview标签,再建一个专为listview单行的显示格式的布局.在重写adapter适配器方法,

public class MyAdapter extends BaseAdapter{

private LayoutInflater mInflater;

private List<Map<String, String>> listData;

private List<Map<String, String>> splitData;

public MyAdapter(Context context,
List<Map<String, String>> listData,
List<Map<String, String>> splitData) {
this.mInflater = LayoutInflater.from(context);
this.listData = listData;
this.splitData = splitData;
}


public int getCount() {
// TODO Auto-generated method stub
return listData.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return listData.get(position);
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//用来禁用标签项的响应事件,false为禁用
@Override
public boolean isEnabled(int position) {
if (splitData.contains(listData.get(position))) {
return false;
}
return super.isEnabled(position);
}

public View getView(int position, View convertView, ViewGroup parent) {

if (splitData.contains(listData.get(position))) {
convertView = mInflater.inflate(R.layout.clazz_tag, null);
} else {
convertView = mInflater.inflate(R.layout.clazz_items, null);
}

TextView textView = (TextView)convertView.findViewById(R.id.class_items);
textView.setText(listData.get(position).get("clazz"));

return convertView;
}

}

上面所做的是有标签的布局,再在activity中创建该adapter的对象

MyAdapter adapter = new MyAdapter(this, clazz_items_list, clazz_tag_list); // 布局里的控件id
// 添加并且显示
list.setAdapter(adapter);

另外可以使用其他android内部所规范的adapter,比如SimpleAdapter.

SimpleAdapter adapter = new SimpleAdapter(this, stu_list,
R.layout.student, new String[] { "id", "name", "sex" },
new int[] { R.id.std_id,R.id.std_name, R.id.std_sex});

下面是继承Activity条件下,ListView对象的监听方法.

list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)

arg2是列表的Item数.

另外关于传递Intent,在书本上找到的都只是些传递简单的int,string类型,而一般要做的都是传递

ArrayList<String> 或ArrayList<Map<..,..>>等,所以要传递整个学生链表及所包含的三个属性,必须讲Student对象序列化(实现Serializable接口),或者封装,传过去的是一个List<Student>,而不是List<Map<String, Object>>,而SimpleAdapter的第二个参数要用到List<Map<String, Object>>.

传递方式:bundle.putSerializable("stud", (Serializable)student_list);

得到的方式:

List<Student> stu_list = (List<Student>)getIntent().getSerializableExtra("stud");
list = Student.getMapList(stu_list);

具体的getMapList的方法是将List<Student>转为List<Map<String,Object>>格式

public static List<Map<String,Object>> getMapList(List<Student> stu_list) {
List<Map<String,Object>> list =new ArrayList<Map<String,Object>>();
for(int i=0;i<stu_list.size();i++)
{
list.add(stu_list.get(i).toMap());
}

return list;
}

静态方法,可通过类名直接调用.

另外还有个将对象封装成 Map<String,Object>的方法,也定义在Student类中:

public Map<String,Object> toMap() {
Map<String,Object> map = new HashMap<String,Object>();

map.put("id", this.getId());
map.put("name", this.getName());
map.put("sex", this.getSex());

return map;

数据的加入:

public static List<Grade> GRADES = new ArrayList<Grade>();


static {
List<Clazz> clazzes = new ArrayList<Clazz>();
List<Student> students = null;
// 添加初中一年级的班级数据和学生数据
Grade grade = new Grade("初中一年级");
Clazz clazz = null;


clazz = new Clazz("初一(1)班");
students = new ArrayList<Student>();
students.add(new Student("2010118", "刘志彬", "男"));
clazz.setStudents(students);
clazzes.add(clazz);

grade.setClazzes(clazzes);

GRADES.add(grade);

最后比较纠结了下的有标签的得到对应班级里的学生的算法:

private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

for(int i =0;i<DataTrunk.GRADES.size();i++)
{
//根据得到的按键项,处理属于哪个班级,并添加进学生表
button_info =button_info-DataTrunk.GRADES.get(i).getClazzes().size()-1;
if(button_info <= 0)
{

button_info =button_info+DataTrunk.GRADES.get(i).getClazzes().size();
for(int j = 0;j<DataTrunk.GRADES.get(i).getClazzes().get(button_info).getStudents().size();j++)
{
student_list.add(DataTrunk.GRADES.get(i).getClazzes().get(button_info).getStudents().get(j));
list.add(DataTrunk.GRADES.get(i).getClazzes().get(button_info).getStudents().get(j).toMap());

}
break;
}

}
return list;
}

posted @ 2011-11-13 23:01  心空,彷徨左右  阅读(109)  评论(0)    收藏  举报