看其他代碼看的我腦袋疼,還是轉身來研究研究Android的東西吧,換換腦子。
這次就來研究研究SimpleAdapter吧,還是老規矩,先從API說起吧。
類的介紹:
public class SimpleAdapter extends BaseAdapter implements Filterable {}
這是一個簡單的適配器,可以將靜態數據映射到XML文件中定義好的視圖。你可以指定數據支持的列表如ArrayList組成的Map。在ArrayList中的每個條目對應List中的一行。Maps包含每行數據。你可以指定一個定義了被用於顯示行的視圖XML文件,通過關鍵字映射到指定的視圖。綁定數據到視圖分兩個階段,首先,如果一個SimpleAdapter.ViewBinder是有效的,setViewValue(android.view.View, Object, String)將被調用。如果返回值是真,綁定完成了。如果返回值爲假,下面的視圖將按以下順序去處理:
l一個實現了Checkable的視圖(例如CheckBox),期望綁定值是一個布爾類型。
lTextView期望綁定值是一個字符串類型,通過調用setViewText(TextView, String)綁定。
lImageView期望綁定值是一個資源id或者一個字符串,通過調用setViewImage(ImageView, int) 或setViewImage(ImageView, String)。
如果沒有一個合適的綁定發生將會拋出IllegalStateException。
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
參數
context 關聯SimpleAdapter運行着的視圖的上下文。
data 一個Map的列表。在列表中的每個條目對應列表中的一行,應該包含所有在from中指定的條目
resource 一個定義列表項目的視圖佈局的資唯一標識。佈局文件將至少應包含哪些在to中定義了的名稱。
from 一個將被添加到Map上關聯每一個項目的列名稱的列表
to 應該在參數from顯示列的視圖。這些應該全是TextView。在列表中最初的N視圖是從參數from中最初的N列獲取的值。
方法:繼承了BaseAdapter可調用所有Baseadapter的方法,不懂得同學可以看我之前寫的關於Baseadapter的使用。下面列舉一些BaseAdapter不存在的方法。
public SimpleAdapter.ViewBinder getViewBinder()
返回被用來綁定數據到視圖的SimpleAdapter.ViewBinder
。
返回值
一個ViewBinder ,如果binder不存在則返回null
參見
setViewBinder(android.widget.SimpleAdapter.ViewBinder)
public void setViewBinder(SimpleAdapter.ViewBinder viewBinder)
設置binder用於綁定數據到視圖
參數
viewBinder 用於綁定數據到視圖的binder可以設置爲null,可用於刪除存在的binder
參見
public void setViewImage(ImageView v, int value)
調用bindView去給ImageView設置圖像,但只有當ViewBinder不存在或者如果存在的ViewBinder無法處理綁定到一個ImageView時才調用。如果提供的數據是一個整形時,setViewImage(ImageView, String)方法將被本方法替代
參數
v 接收圖像的ImageView
value 從數據集獲取數據到值
參見
setViewImage(ImageView, String)
public void setViewImage(ImageView v, String value)
調用bindView去給ImageView設置圖像,但只有當ViewBinder不存在或者如果存在的ViewBinder無法處理綁定到一個ImageView時才調用。默認的,這個值被作爲一個圖像資源來對待。如果這個值作爲一個圖像的Uri來使用。如果提供的數據不是一個整形時,setViewImage(ImageView, int)方法將被本方法替代
參數
v 接收圖像的ImageView
value 從數據集獲取數據到值
參見
public void setViewText(TextView v, String text)
調用bindView去給TextView設置文本,但只有當ViewBinder不存在或者如果存在的ViewBinder無法處理綁定到一個TextView時才調用
參數
v 將接收文本的TextView
text 被設置到TextView的文本
好了,文檔就介紹到這裏,下面開始實例吧。
主佈局文件:
<ListView
android:id="@+id/lvcsdn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner1"
>
</ListView>
listview item 佈局文件:<?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" >
<TextView
android:id="@+id/tvcslayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SimpleAdapter測試"
android:textSize="18sp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測試" />
<ImageView
android:id="@+id/ivsimple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
接下來是測試的activity
public class SimpleAdapterActivity extends Activity{
private String[] names={"張三","李四","王五","趙六"};
private int[] imageids={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView) findViewById(R.id.lvcsdn);
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
for(int i=0;i<names.length;i++){
Map<String,Object> map=new HashMap<String, Object>();
map.put("name", names[i]);
map.put("image", imageids[i]);
list.add(map);
}
SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.csarrayada, new String[]{"name","image"},new int[]{R.id.tvcslayout,R.id.ivsimple} );
lv.setAdapter(adapter);
}
}
無圖無真相,接下來上一下測試的實圖:
至此,SimpleAdapter 的簡單使用就完成了。