文章目录

ListView需要使用适配器装载数据,而最常用的四种容器是ArrayAdapter,SimpleAdapter,BaseAdapter,SimpleCursorAdapter。通过简单的例子介绍这四种适配器使用,来熟悉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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.carlos.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {

private ListView listView1;
private ListView listView2;
private ListView listView3;
private ListView listView4;
private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView1 = getViewById(R.id.listview1);
listView2 = getViewById(R.id.listview2);
listView3 = getViewById(R.id.listview3);
listView4 = getViewById(R.id.listview4);

// android.R.layout.test_list_item,使用的是Android SDK自带的布局
String[] datas = { "a", "b", "c" };
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.test_list_item, datas);
listView1.setAdapter(arrayAdapter);

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("name", "王北");
map.put("age", "20");
list.add(map);
map = new HashMap<String, String>();
map.put("name", "赵西");
map.put("age", "23");
list.add(map);
map = new HashMap<String, String>();
map.put("name", "刘北方");
map.put("age", "21");
list.add(map);
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
list, R.layout.item, new String[] { "name", "age" }, new int[] {
R.id.name, R.id.age });
listView2.setAdapter(simpleAdapter);

// 通过Cursor查找联系人名字和电话号码
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null); // 获取手机联系人
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_expandable_list_item_2, cursor,
new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }, new int[] {
android.R.id.text1, android.R.id.text2 });
listView3.setAdapter(cursorAdapter);

Adapter adapter = new Adapter(this, list);
listView4.setAdapter(adapter);

}

// 通过泛型简化findViewById
<T extends View> T getViewById(int id) {
try {
return (T) findViewById(id);
} catch (ClassCastException e) {
Log.e(TAG, "Could not cast View to create class.", e);
throw e;
}
}

}

外部自定义的一个BaseAdapter供主类调用

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.carlos.listview;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Adapter extends BaseAdapter{

private Context context;
private List<Map<String, String>> list;

//创建构造函数供外部类调用
public Adapter(Context context, List<Map<String, String>> list) {
super();
this.context = context;
this.list = list;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int arg0) {
return list.get(arg0);
}

@Override
public long getItemId(int arg0) {
return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

ViewHolder holder;
if (arg1==null) {
holder=new ViewHolder();
arg1=LayoutInflater.from(context).inflate(R.layout.item, null);
holder.name=(TextView) arg1.findViewById(R.id.name);
holder.age=(TextView) arg1.findViewById(R.id.age);
arg1.setTag(holder);
}else {
holder=(ViewHolder) arg1.getTag();
}
holder.name.setText(list.get(arg0).get("name"));
holder.age.setText(list.get(arg0).get("age"));
return arg1;
}

class ViewHolder{
TextView name;
TextView age;
}

}

主布局

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
46
47
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ArrayAdapter的例子:"
/>
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SimpleAdapter的例子:"
/>
<ListView
android:id="@+id/listview2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SimpleCursorAdapter的例子:"
/>
<ListView
android:id="@+id/listview3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BaseAdapter的例子:"
/>
<ListView
android:id="@+id/listview4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

自定义的ListView的item.xml布局

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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"
/>
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
/>
</LinearLayout>

</LinearLayout>

由于用到查找手机联系人信息,所以AndroidMainfest.xml里面需加上下面这条读取联系人权限,不然会报错

1
<uses-permission android:name="android.permission.READ_CONTACTS" />

运行结果

文章目录