標籤:listview android開發 移動端開發 遊響雲停工作室 android適配器
Android中顯示資料有多種控制項,這節我們來認識下ListView,ListView是Android中最常用的資料顯示控制項,可以顯示簡單資料來源,也可以顯示複雜資料來源,我們在Android系統中常看到的清單項目,基本都是ListView的功勞。ListView中顯示資料,肯定要綁定資料來源。資料來源的綁定是通過Adapter來完成的,Android中有兩種常用的適配器,ArrayAdapter(數組適配器) SimpleAdapter(簡單適配器),適配器的作用就是把複雜的資料來源顯示到istview介面視圖上,是資料來源和介面之間的橋樑。
這一節我們來認識下這兩個適配器,數組適配器用來顯示簡單的資料,簡單適配器主要用來顯示複雜的資料。
1. 數組適配器ArrayAdapter
數組適配器顯示的資料比較單一,我們看下面的例子
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>
package com.example.hellotest;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class FirstListView extends Activity { private ListView lv; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.firstlistview); lv=(ListView)findViewById(R.id.listView1);//擷取Listview對象 //listview資料來源 String[] arr={"遊響雲停工作室1","遊響雲停工作室2","遊響雲停工作室ArrayAdapter示範","遊響雲停交流群207464864"}; //初始化適配器,參數1是內容物件,參數2是Listview中每個列表的布局檔案,參數3是資料來源 adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr); lv.setAdapter(adapter);//綁定資料 }}
我們來分析下數組適配器的參數
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
第一個參數為當前內容物件
第二個參數為布局檔案,我們例子中使用的系統內建的布局檔案
第三個參數是資料來源
2. 簡單適配器SimpleAdapter
簡單適配器用來顯示複雜的資料,我們看下這個樣本
首先建立一個LISTVIEW中每項的布局檔案listitem.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:orientation="vertical" android:gravity="top" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="7" android:gravity="center" > <ImageView android:id="@+id/pic" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/ic_launcher" /></LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="3" > <TextView android:id="@+id/tvname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="商品名稱:" /> <TextView android:id="@+id/tvprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="商品價格:" /> <TextView android:id="@+id/tvcolor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:text="商品顏色" /></LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="2dp" android:background="#F0F0F0"> </LinearLayout></LinearLayout>
首頁面布局檔案
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView2" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>
package com.example.hellotest;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;public class SimpleListView extends Activity { private ListView lv; private SimpleAdapter adp;//定義適配器 private List<Map<String,Object>> mapList;//定義資料來源 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simplelistview); lv=(ListView)findViewById(R.id.listView2); /* * 參數1是內容物件 * 參數2是資料來源 * 參數3是布局檔案 * 參數4是鍵名 * 參數5是綁定布局檔案中視圖ID * * */ mapList=new ArrayList<Map<String,Object>>(); for(int i=0;i<10;i++) { Map<String,Object> map=new HashMap<String,Object>(); map.put("pic",R.raw.pad); map.put("name","商品名稱:Ipad Air"); map.put("price","商品價格:$"+i); map.put("color","商品顏色:白色"); mapList.add(map); } adp=new SimpleAdapter(this, mapList,R.layout.listitem, new String[]{"pic","name","price","color"}, new int[]{R.id.pic,R.id.tvname,R.id.tvprice,R.id.tvcolor}); lv.setAdapter(adp); }}
我們來分析下簡單適配器的參數
adp=new SimpleAdapter(this, mapList,R.layout.listitem, new String[]{"pic","name","price","color"}, new int[]{R.id.pic,R.id.tvname,R.id.tvprice,R.id.tvcolor});
第一個參數是內容物件
第二個參數是資料來源,資料來源的類型是集合
第三個參數是ListView中每一項的布局檔案
第四個參數是數組,數組裡面每一項對應資料來源中MAP的鍵名稱
第五個參數就是ListVIew中對應的子布局檔案中對應的控制項的ID
下載:http://download.csdn.net/detail/zx13525079024/8139657
.Net程式員玩轉Android開發---(12)ListView顯示資料