ListView控件的使用

ListView是一个用于列表显示数据的高级控件。

使用ListView控件的前提条件是每个条目都非常类似。

使用ListView必须要有适配器,除了可以自定义适配器,常用的适配器还有ArrayAdapter(数组适配器)和SimpleAdapter(简单适配器),这些适配器都是继承了BaseAdapter,实现BaseAdapter中的getCount(),getItem(int position),getItemId(int position),getView(int position, View convertView, ViewGroup parent)这四个方法。

##ArrayAdapter的使用##

1
2
3
4
5
6
7
8
9
10
11
//数组适配器
String[] objects = new String[]{"a","b","c","d","e","f","g","h","i"};
ListView lv = (ListView) findViewById(R.id.lv);

//ArrayAdapter只能显示文本控件 并且只能是一个文本控件
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,//上下文
android.R.layout.simple_list_item_1,//条目的布局 android系统底层会有一些简单的布局 引用android底层的资源
android.R.id.text1,//条目里面的控件的id
objects);//要显示的数据
lv.setAdapter(adapter);
//如果以后开发看到简单的文字列表显示那么使用ArrayAdapter是最方便

##SimpleAdapter的使用##

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//简单适配器
String[] objects = new String[]{"a","b","c","d","e","f","g","h","i"};
ListView lv = (ListView) findViewById(R.id.lv);

String[] from = new String[]{"name","age"};
int[] to = new int[]{android.R.id.text1,android.R.id.text2};
final SimpleAdapter adapter = new SimpleAdapter(this,//上下文
data,//数据 List<? extends Map<String, ?>> data
android.R.layout.simple_list_item_2,//条目的布局文件
from,//String[] from 数据从哪里来
to);//int[] to
lv.setAdapter(adapter);
//给ListView的条目设置条目点击监听
lv.setOnItemClickListener(new OnItemClickListener() {
//点击条目的时候,方法别调用 int position 条目的位置
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//适配器提供了一个方法getItem(position) 可以获取条目对应的数据
Map<String,Object> map = (Map<String, Object>) adapter.getItem(position);
String text = "姓名:"+map.get("name")+",age:"+map.get("age");
Toast.makeText(getApplicationContext(), text, 1).show();
}
});

##开发中ListView的使用步骤##

  1. 在布局中写好ListView控件;
  2. 使用findViewById(R.id.lv)获取空间;
  3. 给ListView设置适配器lv.setAdapter();

其中重点和难点都是给ListView设置适配器这部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//自定义适配器继承BaseAdapter
private class MyAdapter extends BaseAdapter{

private Context context;
private List<Student> students;
//布局加载器
private LayoutInflater mInflater;
public MyAdapter(Context context, List<Student> students) {
super();
this.context = context;
this.students = students;
mInflater = LayoutInflater.from(context);
}

//获取ListView条目的数量
public int getCount() {
return students.size();
}

public Object getItem(int position) {
return students.get(position);
}

public long getItemId(int position) {
return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
//使用布局加载器加载布局文件,此处的ViewGroup指向ListView,只是加载布局,而不是加在内容,因此应为null
View view = mInflater.inflate(R.layout.item, null);
//从view中获取对应的控件
//findViewById(id); findViewById(id)是加载Activity布局文件的控件,而此处是从view中获取对应控件,因此一定不能忘记要使用下面的方式获取,前面要加上view
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_age = (TextView) view.findViewById(R.id.tv_age);

//获取条目对应的数据
Student student = students.get(position);

//把数据绑定给指定的控件
tv_name.setText(student.name);
//age是int类型,而setText()方法传入的参数有两种形式,一种是String,直接将需要的属性传进去,还有一种是int,这个int指的是resid,即资源id,会出现资源找不到异常
tv_age.setText(student.age+"");
//返回view,一定要返回view,否则会出现空指针异常
return view;
}

上面的代码里,其中创建ListView显示的条目又可以分为以下几个步骤:

View getView() {

  • 使用布局加载器加载布局;
  • 找到布局中的控件;
  • 找到条目对应的数据;
  • 把数据绑定给对应控件;
  • 返回条目

}