【Android学习专题】控件组件篇:ListView汇总
【Android学习专题】控件组件篇:ListView汇总
SkySeraph Feb 21st 2012 SZTCL
Email:zgzhaobo@gmail.com QQ:452728574
-------------------------------------------------------------------------------------------------------------------------------------------------------------
一、界面效果
-------------------------------------------------------------------------------------------------------------------------------------------------------------
二、步骤
①数据准备:直接用一维或多维动态数组保存数据;构造对象通过toString方式展示数据;联系人数据库
②适配器选择:ArrayAdapter;SimpleAdapter;SimpleCursorAdapter
③ListView显示数据
④事件响应(设置监听器实现单击、滚动、单击长按响应等)
-------------------------------------------------------------------------------------------------------------------------------------------------------------
三、源码
1 总括
①总布局文件:listview_demo.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="wrap_content"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:gravity="center_horizontal"
11 android:paddingBottom="25dp"
12 android:paddingTop="15dp"
13 android:text="SkySeraph学习专题:ListView"
14 android:textColor="#FFFF00"
15 android:textSize="20dp" >
16 </TextView>
17
18 <LinearLayout
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:orientation="vertical"
22 android:layout_gravity="center">
23
24 <Button
25 android:id="@+id/listview_demo_btn01"
26 android:layout_width="fill_parent"
27 android:layout_height="wrap_content"
28 android:text="ListView+ArrayAdapter实现一条信息显示" >
29 </Button>
30
31 <Button
32 android:id="@+id/listview_demo_btn02"
33 android:layout_width="fill_parent"
34 android:layout_height="wrap_content"
35 android:text="ListView+ArrayAdapter实现自定义多条信息显示" >
36 </Button>
37
38 <Button
39 android:id="@+id/listview_demo_btn03"
40 android:layout_width="fill_parent"
41 android:layout_height="wrap_content"
42 android:text="ListView+SimpleCursorAdapter" >
43 </Button>
44
45 <Button
46 android:id="@+id/listview_demo_btn04"
47 android:layout_width="fill_parent"
48 android:layout_height="wrap_content"
49 android:text="ListView+SimpleAdapter" >
50 </Button>
51
52 <Button
53 android:id="@+id/listview_demo_btn05"
54 android:layout_width="fill_parent"
55 android:layout_height="wrap_content"
56 android:text="继承ListActivity" >
57 </Button>
58 </LinearLayout>
59
60 </LinearLayout>
②主java源码:listview_demo.java
1 public class listview_demo extends Activity
2 {
3 @Override
4 protected void onCreate(Bundle savedInstanceState)
5 {
6 // TODO Auto-generated method stub
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.listview_demo);
9 findViews();
10 }
11
12 private void findViews()
13 {
14 /*1*/
15 Button btn01 = (Button) findViewById(R.id.listview_demo_btn01);
16 btn01.setOnClickListener(new OnClickListener()
17 {
18 @Override
19 public void onClick(View v)
20 {
21 // TODO Auto-generated method stub
22 Intent intent01 = new Intent(listview_demo.this,listview_demo01.class);
23 startActivity(intent01);
24 return;
25 }
26 });
27 /*2*/
28 Button btn02 = (Button) findViewById(R.id.listview_demo_btn02);
29 btn02.setOnClickListener(new OnClickListener()
30 {
31 @Override
32 public void onClick(View v)
33 {
34 // TODO Auto-generated method stub
35 Intent intent02 = new Intent(listview_demo.this,listview_demo02.class);
36 startActivity(intent02);
37 return;
38 }
39 });
40 /*3*/
41 Button btn03 = (Button) findViewById(R.id.listview_demo_btn03);
42 btn03.setOnClickListener(new OnClickListener()
43 {
44 @Override
45 public void onClick(View v)
46 {
47 // TODO Auto-generated method stub
48 Intent intent03 = new Intent(listview_demo.this,listview_demo03.class);
49 startActivity(intent03);
50 return;
51 }
52 });
53 /*4*/
54 Button btn04 = (Button) findViewById(R.id.listview_demo_btn04);
55 btn04.setOnClickListener(new OnClickListener()
56 {
57 @Override
58 public void onClick(View v)
59 {
60 // TODO Auto-generated method stub
61 Intent intent04 = new Intent(listview_demo.this,listview_demo04.class);
62 startActivity(intent04);
63 return;
64 }
65 });
66 /*5*/
67 Button btn05 = (Button) findViewById(R.id.listview_demo_btn05);
68 btn05.setOnClickListener(new OnClickListener()
69 {
70 @Override
71 public void onClick(View v)
72 {
73 // TODO Auto-generated method stub
74 Intent intent05 = new Intent(listview_demo.this,listview_demo05.class);
75 startActivity(intent05);
76 return;
77 }
78 });
79 }
80 }
③注意:
需要在AndroidManifest文件中添加五个listview_demo的activity
并需要添加权限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
-------------------------------------------------------------------------------------------------------------------------------------------------------------
2 测试1:ListView+ArrayAdapter实现一条信息显示
①效果:
②布局文件:listview_demo01.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="wrap_content"
5 android:orientation="vertical" >
6
7 <ListView
8 android:id="@+id/listview_demo01_list"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent" />
11
12 </LinearLayout>
③java源码:listview_demo01.java
1 public class listview_demo01 extends Activity implements OnItemClickListener
2 {
3 // ////////////////////////////////////////////////////////////////////////////////////
4 ListView m_ListView;
5 // ////////////////////////////////////////////////////////////////////////////////////
6 //①数据来源:方式一
7 final String[] data =
8 { "first", "secend", "third", "fourth", "fifth" };
9 //①数据来源:方式二
10 private List<String> getData()
11 {
12 List<String> data = new ArrayList<String>();
13 data.add("first");
14 data.add("Second");
15 data.add("Third");
16 data.add("Fourth");
17 data.add("Fifth");
18 return data;
19 }
20 // ////////////////////////////////////////////////////////////////////////////////////
21 @Override
22 protected void onCreate(Bundle savedInstanceState)
23 {
24 // TODO Auto-generated method stub
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.listview_demo01);
27 findViews();
28 }
29
30 // ////////////////////////////////////////////////////////////////////////////////////
31 private void findViews()
32 {
33 m_ListView = (ListView) findViewById(R.id.listview_demo01_list);
34 //②建立适配器
35 /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
36 android.R.layout.simple_list_item_1, data);*/
37 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
38 android.R.layout.simple_list_item_1, getData());
39 /*android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字*/
40 //③显示
41 m_ListView.setAdapter(adapter); //注册
42 //④事件
43 m_ListView.setOnItemClickListener(this);//设置监听器
44 }
45 // ////////////////////////////////////////////////////////////////////////////////////
46 @Override
47 public void onItemClick(AdapterView<?> parent, View view, int position, long id)
48 {
49 // TODO Auto-generated method stub
50 Log.i("TAG",
51 data[position] + "postion=" + String.valueOf(position) + "row_id="
52 + String.valueOf(id));
53 }
54 // ////////////////////////////////////////////////////////////////////////////////////
55 }
④说明:事件采用了单击事件并通过Log输出
-------------------------------------------------------------------------------------------------------------------------------------------------------------
3 测试2:ListView+ArrayAdapter实现自定义多条信息显示
①效果:
②布局文件:listview_demo02.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="wrap_content"
5 android:orientation="vertical" >
6
7 <ListView
8 android:id="@+id/listview_demo02_list"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent" />
11
12 </LinearLayout>
③java源码:listview_demo02.java
1 public class listview_demo02 extends Activity
2 {
3 // ////////////////////////////////////////////////////////////////////////////////////
4 Person[] data = new Person[]
5 { new Person("蔡志坤", 25, "ffczk86@gmail.com", "厦门市"), new Person("李杰华", 25, "aa@bb.com", "漳州市"),
6 new Person("张亮", 25, "cc@gmail.com", "厦门市"),
7 new Person("陈旭", 25, "ccadd@gmail.com", "厦门市"),
8 new Person("刘玄德", 25, "ffczk86@gmail.com", "福州市") };
9
10 // ////////////////////////////////////////////////////////////////////////////////////
11 @Override
12 protected void onCreate(Bundle savedInstanceState)
13 {
14 // TODO Auto-generated method stub
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.listview_demo02);
17 findViews();
18 }
19
20 private void findViews()
21 {
22 // ////////////////////////////////////////////////////////////////////////////////////
23 ListView listView = (ListView) findViewById(R.id.listview_demo02_list);
24 ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this,
25 android.R.layout.simple_list_item_1, data);// 建立适配器
26 listView.setAdapter(adapter);// 注册
27 // ////////////////////////////////////////////////////////////////////////////////////
28 }
29 // ////////////////////////////////////////////////////////////////////////////////////
30 public class Person
31 {
32 public String name;
33 public int age;
34 public String email;
35 public String address;
36
37 public Person(String name, int age, String email, String address)
38 {
39 super();
40 this.name = name;
41 this.age = age;
42 this.email = email;
43 this.address = address;
44 }
45 @Override
46 public String toString() //关键
47 {
48 return "Person [name=" + name + ", age=" + age + ", email=" + email + ", address="
49 + address + "]";
50 }
51 }
52 // ////////////////////////////////////////////////////////////////////////////////////
53 }
④说明:
此部分源码参考:http://stephen830.iteye.com/blog/1139917
-------------------------------------------------------------------------------------------------------------------------------------------------------------
4 测试3:ListView+SimpleCursorAdapter
①效果:
②布局文件:无
③java源码:listview_demo03.java
1 public class listview_demo03 extends Activity
2 {
3 // ////////////////////////////////////////////////////////////////////////////////////
4 ListView m_ListView;
5 // ////////////////////////////////////////////////////////////////////////////////////
6 @Override
7 protected void onCreate(Bundle savedInstanceState)
8 {
9 // TODO Auto-generated method stub
10 super.onCreate(savedInstanceState);
11 // setContentView(R.layout.listview_demo03);
12 findViews();
13 }
14
15 // ////////////////////////////////////////////////////////////////////////////////////
16 private void findViews()
17 {
18 m_ListView = new ListView(this);// 创建ListView对象
19 m_ListView.setBackgroundColor(Color.BLACK);
20 //①数据来源:获取系统通讯录数据库Phones的Cursor
21 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
22 null, null, null);
23 startManagingCursor(cursor);//将获得的Cursor对象交由Activity管理,使得Cursor的生命周期和Activity便能够自动同步
24 //②建立适配器
25 ListAdapter listAdapter = new SimpleCursorAdapter(this,
26 android.R.layout.simple_expandable_list_item_1, cursor, new String[]
27 { PhoneLookup.DISPLAY_NAME }, // 从数据库的NAME中取数据
28 new int[]
29 { android.R.id.text1 }); // 与NAME对应的Views
30 //③显示
31 m_ListView.setAdapter(listAdapter);//将adapter添加到m_ListView中
32 setContentView(m_ListView);// 注册:将adapter添加到m_ListView中
33 //④事件
34 m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
35 {
36 @Override
37 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
38 {
39
40 DisplayToast("滚动到第" + Long.toString(arg0.getSelectedItemId()) + "项");
41 }
42
43 @Override
44 public void onNothingSelected(AdapterView<?> arg0)
45 {
46 // 没有选中
47 }
48 });
49 m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
50 {
51 @Override
52 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
53 {
54 DisplayToast("选中了第" + Integer.toString(arg2 + 1) + "项");
55 }
56 });
57 }
58
59 // ////////////////////////////////////////////////////////////////////////////////////
60 /* 显示Toast子函数*/
61 public void DisplayToast(String str)
62 {
63 Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
64 }
65 // ////////////////////////////////////////////////////////////////////////////////////
66 }
④说明:事件采用了单击和滚动事件并通过Toast显示输出
-------------------------------------------------------------------------------------------------------------------------------------------------------------
5 测试4:ListView+SimpleAdapter
①效果:
②布局文件:listview_demo04.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="horizontal" >
6
7 <ImageView
8 android:id="@+id/listview_demo04_img"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:layout_margin="5px" />
12
13 <LinearLayout
14 android:layout_width="wrap_content"
15 android:layout_height="wrap_content"
16 android:orientation="vertical" >
17
18 <TextView
19 android:id="@+id/listview_demo04_title"
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:textColor="#FFFFFFFF"
23 android:textSize="22px" />
24
25 <TextView
26 android:id="@+id/listview_demo04_info"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:textColor="#FFFFFFFF"
30 android:textSize="13px" />
31 </LinearLayout>
32
33 </LinearLayout>
③java源码:listview_demo04.java
1 public class listview_demo04 extends Activity
2 {
3 // ////////////////////////////////////////////////////////////////////////////////////
4 ListView m_ListView;
5
6 // ////////////////////////////////////////////////////////////////////////////////////
7 @Override
8 protected void onCreate(Bundle savedInstanceState)
9 {
10 // TODO Auto-generated method stub
11 super.onCreate(savedInstanceState);
12 // setContentView(R.layout.listview_demo04);
13 findViews();
14 }
15
16 // ////////////////////////////////////////////////////////////////////////////////////
17 private void findViews()
18 {
19 m_ListView = new ListView(this);// 创建ListView对象
20 // m_ListView = (ListView) findViewById(R.id.**);
21 // ②生成适配器,数组===》ListItem
22 SimpleAdapter mSchedule = new SimpleAdapter(this, //
23 getData1(),// 数据来源
24 R.layout.listview_demo04,// ListItem的XML实现
25 new String[]
26 // 动态数组与ListItem对应的子项
27 { "title", "info", "img" }, new int[]
28 // ListItem的XML文件里面的两个TextView ID
29 { R.id.listview_demo04_title, R.id.listview_demo04_info, R.id.listview_demo04_img });
30 // ③ 显示
31 m_ListView.setAdapter(mSchedule);// 将adapter添加到m_ListView中
32 setContentView(m_ListView);
33 // ④ 事件
34 // 滚动
35 m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
36 {
37 @Override
38 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
39 {
40 DisplayToast("滚动到第" + Long.toString(arg0.getSelectedItemId()) + "项");
41 }
42
43 @Override
44 public void onNothingSelected(AdapterView<?> arg0)
45 {
46 // 没有选中
47 }
48 });
49 // 点击
50 m_ListView.setOnItemClickListener(new OnItemClickListener()
51 {
52 @Override
53 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
54 {
55 setTitle("点击第" + arg2 + "个项目");
56 DisplayToast("选中了第" + Integer.toString(arg2 + 1) + "项");
57 }
58 });
59 // 长按点击
60 m_ListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener()
61 {
62 @Override
63 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
64 {
65 menu.setHeaderTitle("长按菜单-ContextMenu");
66 menu.add(0, 0, 0, "弹出长按菜单0");
67 menu.add(0, 1, 0, "弹出长按菜单1");
68 }
69 });
70 }
71
72 // ////////////////////////////////////////////////////////////////////////////////////
73 /*显示Toast子函数*/
74 public void DisplayToast(String str)
75 {
76 Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
77 }
78
79 // ////////////////////////////////////////////////////////////////////////////////////
80 /*长按菜单响应函数*/
81 @Override
82 public boolean onContextItemSelected(MenuItem item)
83 {
84 setTitle("点击了长按菜单里面的第" + item.getItemId() + "个项目");
85 return super.onContextItemSelected(item);
86 }
87
88 // ////////////////////////////////////////////////////////////////////////////////////
89 // ①数据来源:方式一(显示图标)
90 private List<Map<String, Object>> getData1()
91 {
92 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
93 Map<String, Object> map = new HashMap<String, Object>();
94 map.put("title", "A1");
95 map.put("info", "SkySeraph-1");
96 map.put("img", R.drawable.qq);
97 list.add(map);
98 map = new HashMap<String, Object>();
99 map.put("title", "A2");
100 map.put("info", "SkySeraph-2");
101 map.put("img", R.drawable.qq);
102 list.add(map);
103 map = new HashMap<String, Object>();
104 map.put("title", "A3");
105 map.put("info", "SkySeraph-3");
106 map.put("img", R.drawable.qq);
107 list.add(map);
108 return list;
109 }
110
111 // ①数据来源:方式二(不显示图标): http://blog.csdn.net/hellogv/article/details/4542668
112 private ArrayList<HashMap<String, String>> getData2()
113 {
114 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
115 for (int i = 0; i < 5; i++)
116 {
117 HashMap<String, String> map = new HashMap<String, String>();
118 map.put("title", "This is title.....");
119 map.put("info", "This is info.....");
120 mylist.add(map);
121 }
122 return mylist;
123 }
124 // ////////////////////////////////////////////////////////////////////////////////////
125 }
④说明:事件采用了单击、滚动和单击长按三种事件并通过Toast显示输出
-------------------------------------------------------------------------------------------------------------------------------------------------------------
6 测试5:继承ListActivity
①效果:
②布局文件:无
③java源码:listview_demo05.java
1 public class listview_demo05 extends ListActivity
2 {
3 @Override
4 protected void onCreate(Bundle savedInstanceState)
5 {
6 // TODO Auto-generated method stub
7 super.onCreate(savedInstanceState);
8 findViews();
9 }
10
11 private void findViews()
12 {
13 ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
14 getData());
15 this.setListAdapter(adapter);
16 }
17
18 @Override
19 protected void onListItemClick(ListView l, View v, int position, long id)
20 {
21 // TODO Auto-generated method stub
22 super.onListItemClick(l, v, position, id);
23 setTitle("点击第" + position + "个项目");
24 DisplayToast("选中了第" + Integer.toString(position + 1) + "项");
25 }
26
27 // ////////////////////////////////////////////////////////////////////////////////////
28 /* 显示Toast子函数 */
29 public void DisplayToast(String str)
30 {
31 Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
32 }
33
34 // ////////////////////////////////////////////////////////////////////////////////////
35 // 数据来源
36 private List<String> getData()
37 {
38 List<String> data = new ArrayList<String>();
39 data.add("From ListActivity");
40 data.add("first");
41 data.add("Second");
42 data.add("Third");
43 data.add("Fourth");
44 data.add("Fifth");
45 return data;
46 }
47 }
④说明:事件采用了单击事件并通过Toast显示输出
-------------------------------------------------------------------------------------------------------------------------------------------------------------
作者:skyseraph
出处:http://www.cnblogs.com/skyseraph/
更多精彩请直接访问SkySeraph个人站点:http://skyseraph.com//
Email/GTalk: zgzhaobo@gmail.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。