轉載自:http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html
在android開發(fā)中ListView是比較常用的組件,它以列表的形式展示具體內(nèi)容,并且能夠根據(jù)數(shù)據(jù)的長度自適應顯示。抽空把對ListView的使用做了整理,并寫了個小例子,如下圖。

列表的顯示需要三個元素:
1.ListVeiw 用來展示列表的View。
2.適配器 用來把數(shù)據(jù)映射到ListView上的中介。
3.數(shù)據(jù) 具體的將被映射的字符串,圖片,或者基本組件。
根據(jù)列表的適配器類型,列表分為三種,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最為簡單,只能展示一行字。SimpleAdapter有最好的擴充性,可以自定義出各種效果。SimpleCursorAdapter可以認為是SimpleAdapter對數(shù)據(jù)庫的簡單結合,可以方面的把數(shù)據(jù)庫的內(nèi)容以列表的形式展示出來。
我們從最簡單的ListView開始:
05 |
public class MyListView extends Activity { |
07 |
private ListView listView; |
10 |
public void onCreate(Bundle savedInstanceState){ |
11 |
super .onCreate(savedInstanceState); |
13 |
listView = new ListView( this ); |
14 |
listView.setAdapter( new ArrayAdapter<String>( this , android.R.layout.simple_expandable_list_item_1,getData())); |
15 |
setContentView(listView); |
20 |
private List<String> getData(){ |
22 |
List<String> data = new ArrayList<String>(); |
23 |
data.add( "測試數(shù)據(jù)1" ); |
24 |
data.add( "測試數(shù)據(jù)2" ); |
25 |
data.add( "測試數(shù)據(jù)3" ); |
26 |
data.add( "測試數(shù)據(jù)4" ); |
上面代碼使用了ArrayAdapter(Context context, int textViewResourceId, List<T> objects)來裝配數(shù)據(jù),要裝配這些數(shù)據(jù)就需要一個連接ListView視圖對象和數(shù)組數(shù)據(jù)的適配器來兩者的適配工作,ArrayAdapter的構造需要三個參數(shù),依次為this,布局文件(注意這里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系統(tǒng)定義好的布局文件只顯示一行文字,數(shù)據(jù)源(一個List集合)。同時用setAdapter()完成適配的最后工作。運行后的現(xiàn)實結構如下圖:

SimpleCursorAdapter
sdk的解釋是這樣的:An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views。簡單的說就是方便把從游標得到的數(shù)據(jù)進行列表顯示,并可以把指定的列映射到對應的TextView中。
下面的程序是從電話簿中把聯(lián)系人顯示到類表中。先在通訊錄中添加一個聯(lián)系人作為數(shù)據(jù)庫的數(shù)據(jù)。然后獲得一個指向數(shù)據(jù)庫的Cursor并且定義一個布局文件(當然也可以使用系統(tǒng)自帶的)。
05 |
public class MyListView2 extends Activity { |
07 |
private ListView listView; |
10 |
public void onCreate(Bundle savedInstanceState){ |
11 |
super .onCreate(savedInstanceState); |
13 |
listView = new ListView( this ); |
15 |
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null , null , null , null ); |
16 |
startManagingCursor(cursor); |
18 |
ListAdapter listAdapter = new SimpleCursorAdapter( this , android.R.layout.simple_expandable_list_item_1, |
20 |
new String[]{People.NAME}, |
21 |
new int []{android.R.id.text1}); |
23 |
listView.setAdapter(listAdapter); |
24 |
setContentView(listView); |
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先獲得一個指向系統(tǒng)通訊錄數(shù)據(jù)庫的Cursor對象獲得數(shù)據(jù)來源。
startManagingCursor(cursor);我們將獲得的Cursor對象交由Activity管理,這樣Cursor的生命周期和Activity便能夠自動同步,省去自己手動管理Cursor。
SimpleCursorAdapter 構造函數(shù)前面3個參數(shù)和ArrayAdapter是一樣的,最后兩個參數(shù):一個包含數(shù)據(jù)庫的列的String型數(shù)組,一個包含布局文件中對應組件id的int型數(shù)組。其作用是自動的將String型數(shù)組所表示的每一列數(shù)據(jù)映射到布局文件對應id的組件上。上面的代碼,將NAME列的數(shù)據(jù)一次映射到布局文件的id為text1的組件上。
注意:需要在AndroidManifest.xml中如權限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
運行后效果如下圖:

SimpleAdapter
simpleAdapter的擴展性最好,可以定義各種各樣的布局出來,可以放上ImageView(圖片),還可以放上Button(按鈕),CheckBox(復選框)等等。下面的代碼都直接繼承了ListActivity,ListActivity和普通的Activity沒有太大的差別,不同就是對顯示ListView做了許多優(yōu)化,方面顯示而已。
下面的程序是實現(xiàn)一個帶有圖片的類表。
首先需要定義好一個用來顯示每一個列內(nèi)容的xml
vlist.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:android = " |
03 |
android:orientation = "horizontal" android:layout_width = "fill_parent" |
04 |
android:layout_height = "fill_parent" > |
07 |
< ImageView android:id = "@+id/img" |
08 |
android:layout_width = "wrap_content" |
09 |
android:layout_height = "wrap_content" |
10 |
android:layout_margin = "5px" /> |
12 |
< LinearLayout android:orientation = "vertical" |
13 |
android:layout_width = "wrap_content" |
14 |
android:layout_height = "wrap_content" > |
16 |
< TextView android:id = "@+id/title" |
17 |
android:layout_width = "wrap_content" |
18 |
android:layout_height = "wrap_content" |
19 |
android:textColor = "#FFFFFFFF" |
20 |
android:textSize = "22px" /> |
21 |
< TextView android:id = "@+id/info" |
22 |
android:layout_width = "wrap_content" |
23 |
android:layout_height = "wrap_content" |
24 |
android:textColor = "#FFFFFFFF" |
25 |
android:textSize = "13px" /> |
下面是實現(xiàn)代碼:
05 |
public class MyListView3 extends ListActivity { |
10 |
public void onCreate(Bundle savedInstanceState) { |
11 |
super .onCreate(savedInstanceState); |
13 |
SimpleAdapter adapter = new SimpleAdapter( this ,getData(),R.layout.vlist, |
14 |
new String[]{ "title" , "info" , "img" }, |
15 |
new int []{R.id.title,R.id.info,R.id.img}); |
16 |
setListAdapter(adapter); |
19 |
private List<Map<String, Object>> getData() { |
20 |
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); |
22 |
Map<String, Object> map = new HashMap<String, Object>(); |
23 |
map.put( "title" , "G1" ); |
24 |
map.put( "info" , "google 1" ); |
25 |
map.put( "img" , R.drawable.i1); |
28 |
map = new HashMap<String, Object>(); |
29 |
map.put( "title" , "G2" ); |
30 |
map.put( "info" , "google 2" ); |
31 |
map.put( "img" , R.drawable.i2); |
34 |
map = new HashMap<String, Object>(); |
35 |
map.put( "title" , "G3" ); |
36 |
map.put( "info" , "google 3" ); |
37 |
map.put( "img" , R.drawable.i3); |
使用simpleAdapter的數(shù)據(jù)用一般都是HashMap構成的List,list的每一節(jié)對應ListView的每一行。HashMap的每個鍵值數(shù)據(jù)映射到布局文件中對應id的組件上。因為系統(tǒng)沒有對應的布局文件可用,我們可以自己定義一個布局vlist.xml。下面做適配,new一個SimpleAdapter參數(shù)一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的組件id,title,info,img。布局文件的各組件分別映射到HashMap的各元素上,完成適配。
運行效果如下圖:

有按鈕的ListView
但是有時候,列表不光會用來做顯示用,我們同樣可以在在上面添加按鈕。添加按鈕首先要寫一個有按鈕的xml文件,然后自然會想到用上面的方法定義一個適配器,然后將數(shù)據(jù)映射到布局文件上。但是事實并非這樣,因為按鈕是無法映射的,即使你成功的用布局文件顯示出了按鈕也無法添加按鈕的響應,這時就要研究一下ListView是如何現(xiàn)實的了,而且必須要重寫一個類繼承BaseAdapter。下面的示例將顯示一個按鈕和一個圖片,兩行字如果單擊按鈕將刪除此按鈕的所在行。并告訴你ListView究竟是如何工作的。效果如下:

vlist2.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:android = " |
03 |
android:orientation = "horizontal" |
04 |
android:layout_width = "fill_parent" |
05 |
android:layout_height = "fill_parent" > |
08 |
< ImageView android:id = "@+id/img" |
09 |
android:layout_width = "wrap_content" |
10 |
android:layout_height = "wrap_content" |
11 |
android:layout_margin = "5px" /> |
13 |
< LinearLayout android:orientation = "vertical" |
14 |
android:layout_width = "wrap_content" |
15 |
android:layout_height = "wrap_content" > |
17 |
< TextView android:id = "@+id/title" |
18 |
android:layout_width = "wrap_content" |
19 |
android:layout_height = "wrap_content" |
20 |
android:textColor = "#FFFFFFFF" |
21 |
android:textSize = "22px" /> |
22 |
< TextView android:id = "@+id/info" |
23 |
android:layout_width = "wrap_content" |
24 |
android:layout_height = "wrap_content" |
25 |
android:textColor = "#FFFFFFFF" |
26 |
android:textSize = "13px" /> |
31 |
< Button android:id = "@+id/view_btn" |
32 |
android:layout_width = "wrap_content" |
33 |
android:layout_height = "wrap_content" |
34 |
android:text = "@string/s_view_btn" |
35 |
android:layout_gravity = "bottom|right" /> |
程序代碼:
005 |
public class MyListView4 extends ListActivity { |
008 |
private List<Map<String, Object>> mData; |
011 |
public void onCreate(Bundle savedInstanceState) { |
012 |
super .onCreate(savedInstanceState); |
014 |
MyAdapter adapter = new MyAdapter( this ); |
015 |
setListAdapter(adapter); |
018 |
private List<Map<String, Object>> getData() { |
019 |
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); |
021 |
Map<String, Object> map = new HashMap<String, Object>(); |
022 |
map.put( "title" , "G1" ); |
023 |
map.put( "info" , "google 1" ); |
024 |
map.put( "img" , R.drawable.i1); |
027 |
map = new HashMap<String, Object>(); |
028 |
map.put( "title" , "G2" ); |
029 |
map.put( "info" , "google 2" ); |
030 |
map.put( "img" , R.drawable.i2); |
033 |
map = new HashMap<String, Object>(); |
034 |
map.put( "title" , "G3" ); |
035 |
map.put( "info" , "google 3" ); |
036 |
map.put( "img" , R.drawable.i3); |
044 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
046 |
Log.v( "MyListView4-click" , (String)mData.get(position).get( "title" )); |
052 |
public void showInfo(){ |
053 |
new AlertDialog.Builder( this ) |
054 |
.setTitle( "我的listview" ) |
056 |
.setPositiveButton( "確定" , new DialogInterface.OnClickListener() { |
058 |
public void onClick(DialogInterface dialog, int which) { |
067 |
public final class ViewHolder{ |
068 |
public ImageView img; |
069 |
public TextView title; |
070 |
public TextView info; |
071 |
public Button viewBtn; |
075 |
public class MyAdapter extends BaseAdapter{ |
077 |
private LayoutInflater mInflater; |
080 |
public MyAdapter(Context context){ |
081 |
this .mInflater = LayoutInflater.from(context); |
084 |
public int getCount() { |
090 |
public Object getItem( int arg0) { |
096 |
public long getItemId( int arg0) { |
102 |
public View getView( int position, View convertView, ViewGroup parent) { |
104 |
ViewHolder holder = null ; |
105 |
if (convertView == null ) { |
107 |
holder= new ViewHolder(); |
109 |
convertView = mInflater.inflate(R.layout.vlist2, null ); |
110 |
holder.img = (ImageView)convertView.findViewById(R.id.img); |
111 |
holder.title = (TextView)convertView.findViewById(R.id.title); |
112 |
holder.info = (TextView)convertView.findViewById(R.id.info); |
113 |
holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn); |
114 |
convertView.setTag(holder); |
118 |
holder = (ViewHolder)convertView.getTag(); |
122 |
holder.img.setBackgroundResource((Integer)mData.get(position).get( "img" )); |
123 |
holder.title.setText((String)mData.get(position).get( "title" )); |
124 |
holder.info.setText((String)mData.get(position).get( "info" )); |
126 |
holder.viewBtn.setOnClickListener( new View.OnClickListener() { |
129 |
public void onClick(View v) { |
下面將對上述代碼,做詳細的解釋,listView在開始繪制的時候,系統(tǒng)首先調用getCount()函數(shù),根據(jù)他的返回值得到listView的長度(這也是為什么在開始的第一張圖特別的標出列表長度),然后根據(jù)這個長度,調用getView()逐一繪制每一行。如果你的getCount()返回值是0的話,列表將不顯示同樣return 1,就只顯示一行。
系統(tǒng)顯示列表時,首先實例化一個適配器(這里將實例化自定義的適配器)。當手動完成適配時,必須手動映射數(shù)據(jù),這需要重寫getView()方法。系統(tǒng)在繪制列表的每一行的時候將調用此方法。getView()有三個參數(shù),position表示將顯示的是第幾行,covertView是從布局文件中inflate來的布局。我們用LayoutInflater的方法將定義好的vlist2.xml文件提取成View實例用來顯示。然后將xml文件中的各個組件實例化(簡單的findViewById()方法)。這樣便可以將數(shù)據(jù)對應到各個組件上了。但是按鈕為了響應點擊事件,需要為它添加點擊監(jiān)聽器,這樣就能捕獲點擊事件。至此一個自定義的listView就完成了,現(xiàn)在讓我們回過頭從新審視這個過程。系統(tǒng)要繪制ListView了,他首先獲得要繪制的這個列表的長度,然后開始繪制第一行,怎么繪制呢?調用getView()函數(shù)。在這個函數(shù)里面首先獲得一個View(實際上是一個ViewGroup),然后再實例并設置各個組件,顯示之。好了,繪制完這一行了。那 再繪制下一行,直到繪完為止。在實際的運行過程中會發(fā)現(xiàn)listView的每一行沒有焦點了,這是因為Button搶奪了listView的焦點,只要布局文件中將Button設置為沒有焦點就OK了。
運行效果如下圖:

源碼下載