當前位置:編程學習大全網 - 源碼破解 - android listview怎麽用

android listview怎麽用

Android listview與adapter用法

listview與adapter用法

壹個ListView通常有兩個職責。

(1)將數據填充到布局。

(2)處理用戶的選擇點擊等操作。

第壹點很好理解,ListView就是實現這個功能的。第二點也不難做到,在後面的學習中讀者會發現,這非常簡單。

壹個ListView的創建需要3個元素。

(1)ListView中的每壹列的View。

(2)填入View的數據或者圖片等。

(3)連接數據與ListView的適配器。

也就是說,要使用ListView,首先要了解什麽是適配器。適配器是壹個連接數據和AdapterView(ListView就是壹個典型的AdapterView,後面還會學習其他的)的橋梁,通過它能有效地實現數據與AdapterView的分離設置,使AdapterView與數據的綁定更加簡便,修改更加方便

Android中提供了很多的Adapter,表4-5列出了常用的幾個。

表4-5 常用適配器

Adapter

含義

ArrayAdapter<T>

用來綁定壹個數組,支持泛型操作

SimpleAdapter

用來綁定在xml中定義的控件對應的數據

SimpleCursorAdapter

用來綁定遊標得到的數據

BaseAdapter

通用的基礎適配器

其實適配器還有很多,要註意的是,各種Adapter只不過是轉換的方式和能力不壹樣而已。下面就通過使用不同的Adapter來為ListView綁定數據(SimpleCursorAdapter暫且不講,後面講SQLite時會介紹)。

4.12.1 ListView使用ArrayAdapter

用ArrayAdapter可以實現簡單的ListView的數據綁定。默認情況下,ArrayAdapter綁定每個對象的toString值到layout中預先定義的TextView控件上。ArrayAdapter的使用非常簡單。

實例:

工程目錄:EX_04_12

在布局文件中加入壹個ListView控件。

<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="

/apk/res/android" android:layout_width="fill_parent"

android:layout_height="fill_parent"> <!-- 添加壹個ListView控件 --> <ListView

android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

</LinearLayout>

然後在Activity中初始化。

publicclass MyListView extends Activity {

privatestaticfinal String[] strs = new String[] {

"first", "second", "third", "fourth", "fifth"

};//定義壹個String數組用來顯示ListView的內容private ListView lv;/** Called when the activity is first created. */

@Override

publicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.main);

lv = (ListView) findViewById(R.id.lv);//得到ListView對象的引用 /*為ListView設置Adapter來綁定數據*/

lv.setAdapter(new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_1, strs));

}

}

▲圖4-29 ListView使用ArrayAdapter運行效果

代碼非常的簡單,運行效果如圖4-29所示。

分析壹下使用的步驟。

(1)定義壹個數組來存放ListView中item的內容。

(2)通過實現ArrayAdapter的構造函數來創建壹個ArrayAdapter的對象。

(3)通過ListView的setAdapter()方法綁定ArrayAdapter。

其中第二步有必要說壹下的是,ArrayAdapter有多個構造函數,例子中實現的是最常用的壹種。第壹個參數為上下文,第二個參數為壹個包含TextView,用來填充ListView的每壹行的布局資源ID。第三個參數為ListView的內容。其中第二個參數可以自定義壹個layout,但是這個layout必須要有TextView控件。通常我們使用Android提供的資源,除了例子中所用的,常用的還有如下幾種,可實現帶RadioButton和CheckBox的ListView。

(1)通過指定android.R.layout.simple_list_item_checked這個資源,實現帶選擇框的ListView。需要用setChoiceMode()方法設定選擇為多選還是單選,否則將不能實現選擇效果,運行效果如圖4-30所示。

實現代碼如下:

lv.setAdapter(new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_checked, strs));

lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

(2)通過指定android.R.layout.simple_list_item_multiple_choice這個資源實現帶CheckBox的ListView。同樣的,需要用setChoiceMode()方法來設置單選或者多選,運行效果如圖4-31所示。

實現代碼如下:

lv.setAdapter(new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_multiple_choice, strs));

lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

(3)通過指定android.R.layout.simple_list_item_single_choice這個資源實現帶RadioButton的ListView。這裏要註意的是,這裏並不是指定了單選。是多選還是單選要通過setChoiceMode()方法來指定,運行效果如圖4-32所示。

實現代碼如下:

lv.setAdapter(newArrayAdapter<String>(this,

android.R.layout.simple_list_item_single_choice,strs));

lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

在前面講到過,ListView的職責除了填充數據外,還要處理用戶的操作。通過如下的代碼就可以為ListView綁定壹個點擊監聽器,點擊後在標題欄顯示點擊的行數。

lv.setOnItemClickListener(new OnItemClickListener() {

@Override

publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

//點擊後在標題上顯示點擊了第幾行 setTitle("妳點擊了第"+arg2+"行");

}

});

4.12.2 ListView使用SimpleAdapter

很多時候需要在列表中展示壹些除了文字以外的東西,比如圖片等。這時候可以使用SimpleAdapter。SimpleAdapter的使用也非常簡單,同時它的功能也非常強大。可以通過它自定義ListView中的item的內容,比如圖片、多選框等。看壹個例子,實現壹個每壹行都有壹個ImageView和TextView的ListView。先看壹下運行效果,如圖4-34所示。

▲圖4-34 帶圖標的ListView

首先在布局文件中增加壹個ListView控件。

還需要定義壹個ListView中每壹行的布局,用RelativeLayout來實現壹個帶兩行字和壹個圖片的布局。

item.xml:

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="/apk/res/android"

android:layout_height="fill_parent" android:layout_width="fill_parent">

<ImageViewandroid:layout_alignParentRight="true" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:id="@+id/ItemImage"/>

<TextViewandroid:id="@+id/ItemTitle" android:layout_height="wrap_content"

android:layout_width="fill_parent" android:textSize="20sp"/>

<TextViewandroid:id="@+id/ItemText" android:layout_height="wrap_content"

android:layout_width="fill_parent" android:layout_below="@+id/ItemTitle"/> </RelativeLayout>

配置完畢,就可以在Java代碼中為ListView綁定數據。

publicclass MyListViewSimple extends Activity {

private ListView lv;

/** Called when the activity is first created. */ @Override

publicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.main);

lv = (ListView) findViewById(R.id.lv);/*定義壹個動態數組*/

ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();/*在數組中存放數據*/

for(int i=0;i<10;i++)

{

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("ItemImage", R.drawable.icon);//加入圖片 map.put("ItemTitle", "第"+i+"行");

map.put("ItemText", "這是第"+i+"行");

listItem.add(map);

}

SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,//需要綁定的數據

R.layout.item,//每壹行的布局//動態數組中的數據源的鍵對應到定義布局的View中new String[] {"ItemImage"

,"ItemTitle", "ItemText"},

newint[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}

);

lv.setAdapter(mSimpleAdapter);//為ListView綁定適配器 lv.setOnItemClickListener(new

OnItemClickListener() {

@Override

publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

setTitle("妳點擊了第"+arg2+"行");//設置標題欄顯示點擊的行

}

});

}

}

使用simpleAdapter的數據壹般都是用HashMap構成的列表,列表的每壹節對應ListView的每壹行。通過SimpleAdapter的構造函數,將HashMap的每個鍵的數據映射到布局文件中對應控件上。這個布局文件壹般根據自己的需要來自己定義。梳理壹下使用SimpleAdapter的步驟。

(1)根據需要定義ListView每行所實現的布局。

(2)定義壹個HashMap構成的列表,將數據以鍵值對的方式存放在裏面。

(3)構造SimpleAdapter對象。

(4)將LsitView綁定到SimpleAdapter上。

4.12.3 ListView使用BaseAdapter與ListView的優化

在ListView的使用中,有時候還需要在裏面加入按鈕等控件,實現單獨的操作。也就是說,這個ListView不再只是展示數據,也不僅僅是這壹行要來處理用戶的操作,而是裏面的控件要獲得用戶的焦點。讀者可以試試用SimpleAdapter添加壹個按鈕到ListView的條目中,會發現可以添加,但是卻無法獲得焦點,點擊操作被ListView的Item所覆蓋。這時候最方便的方法就是使用靈活的適配器BaseAdapter了。

使用simpleAdapter的數據壹般都是用HashMap構成的列表,列表的每壹節對應ListView的每壹行。通過SimpleAdapter的構造函數,將HashMap的每個鍵的數據映射到布局文件中對應控件上。這個布局文件壹般根據自己的需要來自己定義。梳理壹下使用SimpleAdapter的步驟。

(1)根據需要定義ListView每行所實現的布局。

(2)定義壹個HashMap構成的列表,將數據以鍵值對的方式存放在裏面。

(3)構造SimpleAdapter對象。

(4)將LsitView綁定到SimpleAdapter上。

4.12.3 ListView使用BaseAdapter與ListView的優化

在ListView的使用中,有時候還需要在裏面加入按鈕等控件,實現單獨的操作。也就是說,這個ListView不再只是展示數據,也不僅僅是這壹行要來處理用戶的操作,而是裏面的控件要獲得用戶的焦點。讀者可以試試用SimpleAdapter添加壹個按鈕到ListView的條目中,會發現可以添加,但是卻無法獲得焦點,點擊操作被ListView的Item所覆蓋。這時候最方便的方法就是使用靈活的適配器BaseAdapter了。

  • 上一篇:村上春樹《世界盡頭與冷酷仙境》經典語錄
  • 下一篇:我學會了用 Scrach編程跳壹跳_800字
  • copyright 2024編程學習大全網