【Android學習專題】控件組件篇:ListView匯總


【Android學習專題】控件組件篇:ListView匯總

   SkySeraph Feb 21st 2012  SZTCL

Email:zgzhaobo@gmail.com    QQ:452728574

-------------------------------------------------------------------------------------------------------------------------------------------------------------

一、界面效果

-------------------------------------------------------------------------------------------------------------------------------------------------------------

二、步驟

①數據准備:直接用一維或多維動態數組保存數據;構造對象通過toString方式展示數據;聯系人數據庫
②適配器選擇:ArrayAdapter;SimpleAdapter;SimpleCursorAdapter
③ListView顯示數據
④事件響應(設置監聽器實現單擊、滾動、單擊長按響應等)

-------------------------------------------------------------------------------------------------------------------------------------------------------------

三、源碼
1 總括

①總布局文件:listview_demo.xml

View Code
 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

View Code
 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

View Code
 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

View Code
 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

View Code
 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

View Code
 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

View Code
 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

View Code
 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

View Code
  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

View Code
 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顯示輸出

-------------------------------------------------------------------------------------------------------------------------------------------------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM