日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Android ListView和ListAdapter

 瞻云軒 2015-05-08
一個ListView顯示出來需要3個東西:
1,listview(用來顯示數(shù)據(jù)的列表)。
2,Data(需要顯示的數(shù)據(jù))。
3,一個綁定Data和Listview的適配器ListAdapter。
一,ListView
1,ListView的每一項其實都是TextView。
2,通過setAdapter方法來調(diào)用一個listAdapter來綁定數(shù)據(jù)。
二,ListAdapter
1,ListAdapter是綁定Data和Listview的適配器。但是,它是接口,需要使用它的子類。
常見的子類有:arrayAdapter,SimpleAdapter ,CursorAdapter
2,android系統(tǒng)默認提供了很多默認的布局方式,也可以自定義。

Android.R.layout.simple_list_item_1:每一項只有一個TextView
Android.R.layout.simple_list_item_2:每一項有兩個TextView
Android.R.layout.simpte.list_item_single_choice,每一項有一個TextView,但是這一項可以被選中。
三,ArrayAdapter
1,數(shù)組適配器,它的作用就是一個數(shù)組和listview之間的橋梁,它可以將數(shù)組里邊定義的數(shù)據(jù)一一對應的顯示在Listview里邊。
2,ListView的每個TextView里邊顯示的內(nèi)容就是數(shù)組里邊的對象調(diào)用toString()方法后生成的字符串。
3,這是一個簡單創(chuàng)建一個list的例子其中的代碼:
ListView listview=new ListView(this);
// 構(gòu)造一個listview對象
String[] data = {“google”,”amazon”,”facebook”};
// 構(gòu)造一個數(shù)組對象,也就是數(shù)據(jù)
listview.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice, data));
//構(gòu)造一個array適配器,然后listview對象通過setAdapter方法調(diào)用適配器來和自己綁定數(shù)據(jù)
list.setItemsCanFocus(true);
list.setChoiceMode(listview.CHOICE_MODE_MULTIPLE);
setContentView(listview);


四,SimpleAdapter
1,作用是ArrayList和ListView的橋梁。這個ArrayList里邊的每一項都是一個Map<String,?>類型。ArrayList當中的每一項Map對象都和ListView里邊的每一項進行數(shù)據(jù)綁定一一對應。
2,SimpleAdapter的構(gòu)造函數(shù):
SimpleAdapter(Context  context, List<? extends Map<String, ?>>  data, int resource, String[]  from, int[] to)
參數(shù):
1,context:上下文。
2,data:基于Map的list。Data里邊的每一項都和ListView里邊的每一項對應。Data里邊的每一項都是一個Map類型,這個Map類里邊包含了ListView每一行需要的數(shù)據(jù)。
3,resource :就是一個布局layout,可引用系統(tǒng)提供的,也可以自定義。
4,from:這是個名字數(shù)組,每個名字是為了在ArrayList數(shù)組的每一個item索引Map<String,Object>的Object用的。
5,to:里面是一個TextView數(shù)組。這些TextView是以id的形式來表示的。例如:Android.R.id.text1,這個text1在layout當中是可以索引的。
3,通過一個類子來理解下:
listitem.xml文件:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas./apk/res/android
android:orientation=”horizontal” android:layout_width=”fill_parent”
android:layout_height=”wrap_content”>
<TextView android:id=”@+id/mview1″ android:layout_width=”100px”
android:layout_height=”wrap_content” />
<TextView android:id=”@+id/mview2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</LinearLayout>
下面是activity文件的部分代碼
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> item;
item = new HashMap<String,Object>();
item.put(“姓名”,”張三”);
item.put(“性別”,”男”);
data.add(item);
item = new HashMap<String,Object>();
item.put(“姓名”,”李四”);
item.put(“性別”,”女”);
data.add(item);
// 上面是構(gòu)造數(shù)據(jù)部分。
ListView listview= new ListView(this);
//構(gòu)造listview對象。
SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.listitem,new String[]{“姓名”,”性別”},new int[]{R.id.TextView01,R.id.TextView02});
/*構(gòu)造一個適配器。
*    1,第三個參數(shù)是說明用的是自定義的布局R.layout.listtiem。
* 2,第四和第五個參數(shù)一起理解:
*          把我們添加數(shù)據(jù)時姓名那一列對應到R.id.TextView01這個TextView中,把性別對應到R.id.TextView02這個TextView中。
*          如果把from改為new String[]{“姓名”,”姓名”},測試下就會明白。
*/
listview.setAdapter(adapter);
setContentView(listview);

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多