- 浏览: 27634 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
a379933101:
哦,没看清,原来是缓冲中的呀
listView中多个listItem布局时 convertView缓存及使用(转) -
a379933101:
getItemViewType有必要用吗,getItemVie ...
listView中多个listItem布局时 convertView缓存及使用(转) -
慢跑学Android:
原来这个name后面不需要加.xml。网上看了一个关 ...
android: SharedPreference
ListView是在Android很常見的一種視圖,ListView不論在電話簿中還是觀看大量的列表資料,都是會用得到。
ListView的使用需要搭配Adapter,Adapter是用來連接資料和ListView的,Adapter除了會用到ListView中,另外會用到還有Spinner(類似下拉選單)的這個元件中。在Google IO 2009的其中一張投影片中,可以很清楚的看到其中的關係圖:
從這張圖就可以看到Adapter基本上會分成Cursor類和ArrayList類的。Cursor和資料庫或目前電話中的資料有關,例如要抓出目前電話簿中的聯絡人資料,用的就是Cursor的Adapter,而自己在程式中建立的陣列資料,用的就會是ArrayList類的Adapter。
最常用的有幾種Adapter:
- ArrayAdapter:將一組數組連繫到ListView
- SimpleAdapter:適用於自訂ListView外觀的應用場合
- BaseAdapter:抽象類別,所以有多個方法需要實作。適用於需要自訂ListView外觀等靈活應用的場合。
- SimpleCursorAdapter:將一組現有的資料庫或聯絡人等ContentProvider的資料查詢的結果連繫到ListView中
首先,要使用ListView可以用ListView這個UI組件,放置到目前的Activity中。另一個可以用的方式,是直接繼承ListActivity,這是一個Activity的子類,其中就會包含一個全螢幕的ListView物件。ListActivity用法比較簡單:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class ListDemo1 extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //注意:不能使用main中的layout,用了會出現錯誤 //setContentView(R.layout.main); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings)); //啟用按鍵過濾功能 getListView().setTextFilterEnabled(true); } private static final String[] mStrings = new String[] { "大餅包小餅", "蚵仔煎", "東山鴨頭", "臭豆腐", "潤餅", "豆花", "青蛙下蛋","豬血糕", "大腸包小腸", "鹹水雞", "烤香腸","車輪餅","珍珠奶茶","鹹酥雞","大熱狗", "炸雞排","山豬肉","花生冰","剉冰","水果冰", "包心粉圓","排骨酥","沙茶魷魚","章魚燒","度小月", "aaa","abc","bbb","bcd","123" }; } |
第8行:這個是對照一般的Activity中有的setContentView方法,因為是ListActivity所以不需要,用了也會有錯誤訊息。
第10行:用setListAdapter方法設定一個ArrayAdapter
第14行:按鍵過濾功能,因為List中的項目有可能很多,像這個範例你按下鍵盤中的a,就會出現以a開頭的項目,方便找到項目
第18~25行:一些要放到List中的字串值
假設是在Activity中除了ListView之外,還要放入其他的組件時,這時候就需要在Activity中加入一個ListView組件,利用這個組件的setAdapter來連接Adapter,範例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class ListDemo2 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //使用main中的layout setContentView(R.layout.main); //找到listview組件 ListView list = (ListView) findViewById(R.id.listView1); //listview物件使用setAdapter方法(比對ListActivity是用setListAdapter) list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings)); //啟用按鍵過濾功能(直接用listview物件,不需要getListView方法) list.setTextFilterEnabled(true); } private static final String[] mStrings = new String[] { "大餅包小餅", "蚵仔煎", "東山鴨頭", "臭豆腐", "潤餅", "豆花", "青蛙下蛋","豬血糕", "大腸包小腸", "鹹水雞", "烤香腸","車輪餅","珍珠奶茶","鹹酥雞","大熱狗", "炸雞排","山豬肉","花生冰","剉冰","水果冰", "包心粉圓","排骨酥","沙茶魷魚","章魚燒","度小月", "aaa","abc","bbb","bcd","123" }; } |
值得一提的是在ArrayAdapter中有一個android定義好的內建list樣式 - "android.R.layout.simple_list_item_1",注意這並不是我們定義的,在android系統中預設就有存在了。其中常用的的這些樣式如下所列:
- android.R.layout.simple_list_item_1:一行text
- android.R.layout.simple_list_item_2:一行text較大,一行text較小
- android.R.layout.simple_list_item_single_choice:單選
- android.R.layout.simple_list_item_multiple_choice:多選按鈕
- android.R.layout.simple_list_item_checked:勾選盒
第1個剛剛有用了,事實上第3,4,5個也是直接換上去就可以看到了,只不過還沒有實作選中後的處理方法。
第2個android.R.layout.simple_list_item_2就比較麻煩了,原因是ArrayAdapter並不支援傳入兩個字串參數值,所以要改用SimpleAdapter,而且傳入的數值型態要改為ArrayList才可以,下面的範例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
public class ListDemo3 extends ListActivity { /** Called when the activity is first created. */ ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); private SimpleAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //把資料加入ArrayList中 for(int i=0; i<mPlaces.length; i++){ HashMap<String,String> item = new HashMap<String,String>(); item.put( "food", mFoods[i]); item.put( "place",mPlaces[i] ); list.add( item ); } //新增SimpleAdapter adapter = new SimpleAdapter( this, list, android.R.layout.simple_list_item_2, new String[] { "food","place" }, new int[] { android.R.id.text1, android.R.id.text2 } ); //ListActivity設定adapter setListAdapter( adapter ); //啟用按鍵過濾功能,這兩行資料都會進行過濾 getListView().setTextFilterEnabled(true); } private static final String[] mPlaces = new String[] { "台北市", "新北市", "台南市", "高雄市", "苗粟縣", "台北市", "新北市", "台南市", "高雄市", "苗粟縣", "台北市", "新北市", "台南市", "高雄市", "苗粟縣", "台北市", "新北市", "台南市", "高雄市", "苗粟縣", "台北市", "新北市", "台南市", "高雄市", "苗粟縣", "台北市", "新北市", "789", "cde", "abc" }; private static final String[] mFoods = new String[] { "大餅包小餅", "蚵仔煎", "東山鴨頭", "臭豆腐", "潤餅", "豆花", "青蛙下蛋","豬血糕", "大腸包小腸", "鹹水雞", "烤香腸","車輪餅","珍珠奶茶","鹹酥雞","大熱狗", "炸雞排","山豬肉","花生冰","剉冰","水果冰", "包心粉圓","排骨酥","沙茶魷魚","章魚燒","度小月", "aaa","abc","bbb","bcd","123" }; } |
執行的結果如下:
如果不要用android內附的simple_list_item_2,改用自己定義的樣式,要怎麼作呢?像上面的範例,再加上一個評分的字串在地點的旁邊。首先先製作一個給List的項目用的layout,如下的xml,取名為mylistview1.xml。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="fill_parent" android:orientation="vertical"> <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="6dip" android:layout_marginTop="6dip" android:textAppearance="?android:attr/textAppearanceLarge"> </TextView> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/linearLayout1" android:orientation="horizontal"> <TextView android:id="@+id/textView2" android:text="TextView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall"> </TextView> <TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_marginLeft="6dip"> </TextView> </LinearLayout> </LinearLayout> |
接下來要改一下程式碼,改用自己定義的item layout,利用R.java裡面的定義就行了(下面程式碼省略了rating這個sting array的定義,記得加上):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//把資料加入ArrayList中 for(int i=0; i<mPlaces.length; i++){ HashMap<String,String> item = new HashMap<String,String>(); item.put( "food", mFoods[i]); item.put( "place","地點:"+mPlaces[i] ); item.put("rating", "評分:"+mRatings[i]+" 星"); list.add( item ); } //新增SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.mylistview1, new String[] { "food","place","rating" }, new int[] { R.id.textView1, R.id.textView2, R.id.textView3 } ); |
執行的結果如下:
下一步,加上圖 片吧…沒圖片很難有真相,圖片需要先放到res/drawable-xxxx目錄中(這裡只有放到res/drawable-hdpi中),抓取圖片用R.drawable.pic,不過因為HashMap的value部份需要用圖片(R.drawable.pic),是一個int的型態,所以HashMap的value部份需要改為Object,才能容得下int和string的類型。先將用於list的項目用的layout改一下,加上圖片在標題的左邊:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/linearLayout1" > <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_width="wrap_content" android:src="@drawable/icon"></ImageView> <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="6dip" android:layout_marginTop="6dip" android:textAppearance="?android:attr/textAppearanceLarge"> </TextView> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/linearLayout1" android:orientation="horizontal"> <TextView android:id="@+id/textView2" android:text="TextView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall"> </TextView> <TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_marginLeft="6dip"> </TextView> </LinearLayout> </LinearLayout> |
再來要改一下程式碼,加上圖片和改Hashmap的value型態為Object(部份程式碼):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>(); private SimpleAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //把資料加入ArrayList中 for(int i=0; i<mPlaces.length; i++){ HashMap<String,Object> item = new HashMap<String,Object>(); item.put("pic", mPics[i]); item.put( "food", mFoods[i]); item.put( "place","地點:"+mPlaces[i] ); item.put("rating", "評分:"+mRatings[i]+" 星"); list.add( item ); } //新增SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.mylistview2, new String[] { "pic","food","place","rating" }, new int[] { R.id.imageView1, R.id.textView1, R.id.textView2, R.id.textView3 } ); //ListActivity設定adapter setListAdapter( adapter ); //啟用按鍵過濾功能,這兩行都會進行過濾 getListView().setTextFilterEnabled(true); } private static final int[] mPics=new int[]{ R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5, R.drawable.pic1,R.drawable.pic2,R.drawable.pic3, R.drawable.pic4,R.drawable.pic5 }; ... |
執行的結果如下:
寫到這裡寫得篇幅有點多了,這只是把一些基本的listview顯示資料的方式記錄下來而已,還沒真正開始進行操作。
以上圖片和文字僅供參考。
发表评论
-
android:随记
2011-11-12 17:16 1565获取联系人信息: Inten ... -
android:sqlite
2011-11-09 16:11 2256android上可以 ... -
android: SharedPreference
2011-11-07 16:50 1458SharedPreferece 可以用来保存配置文件,后缀名为 ... -
android:handler(转)
2011-11-02 16:50 806Handler的定义: ... -
listView中多个listItem布局时 convertView缓存及使用(转)
2011-10-31 23:40 2744最近有需求需要在listView中载入不同的listIt ... -
获取手机的分辨率
2011-10-31 14:45 776DisplayMetrics dm; dm = new Di ...
相关推荐
Android listview与adapter操作和用法
基于Eclipse开发平台,Android中使用ListView和三种适配器(ArrayAdaper,SimpleAdapter,BaseAdapter)实现数据的填充。
Android listview例子, 采用自定义Adapter:包含了Button控件
Android自定义adapter的listview代码
在 Android中 如果使用到了 ListView 或GridView 一定要 给其 添加 适配器Adapter 但 每次 写对应的 适配器时,其中很多方法都是 重复的 , 所以我这个demo 将其封装了起来,使用的时候,只需继承 我封装的类,然后...
Android简单使用Listview和Adapter实现数据显示,通过按钮删除ListView中的子项
android listview的一个例子。使用了java的反射功能,实现自动构建adapter,不需要自己从BaseAdapter继承
Android ListView 自定义adapter好友列表
Android之自定义Adapter的ListView
Android listview 基本步骤 一个小事例
Android ListView adapter不同布局,当需要用到ListView叠加嵌套时候可以考虑
ListView与BaseAdapter的配套使用 理解android 中adapter适配的作用 以及ListView的优化方式
listview和recycleview的adapter的封装,考虑重用性和可读性,不过度抽象
C# xamarin android listview简单使用,希望能够帮助到大家。
本节课程内容:ListView的介绍 1.1 ListView控件 1.2 常用数据适配器(Adapter) 数据展示控件 ListView控件 ListView控件 The ListView control ……> <ListView android:id="@+id/lv" android:layout_width=...
android ListView item下载 ListView 进度条下载 ListView多个下载 android:ListView的局部刷新 Android中自定义Adapter实现ListView动态刷新进度条 android listview局部刷新和模拟应用下载
在实际开发中,很多项目都用到了,listView添加头部的写法,疫情我以为很难,最近偶然发现很简单,所以下了一个例子和大家交流
it's a normal Android listview adapter.The adapter extends BaseAdapter...
android ListView item下载 ListView 进度条下载 ListView多个下载 android:ListView的局部刷新 Android中自定义Adapter实现ListView动态刷新进度条 android listview局部刷新和模拟应用下载
Android ListView 详解,适配器adapter详解 博客地址:http://blog.csdn.net/csdnyuandaimaxuexi/article/details/48808303