這是一個簡單的適配器,可以將待用資料映射到XML檔案中定義好的視圖。你可以指定資料支援的列表如ArrayList組成的Map。在ArrayList中的每個條目對應List中的一行。Maps包含每行資料。你可以指定一個定義了被用於顯示行的視圖XML檔案,通過關鍵字映射到指定的視圖。綁定資料到視圖分兩個階段,首先,如果一個SimpleAdapter.ViewBinder是有效,setViewValue(android.view.View, Object, String)將被調用。如果傳回值是真,綁定完成了。如果傳回值為假,下面的視圖將按以下順序去處理:
一個實現了Checkable的視圖(例如CheckBox),期望綁定值是一個布爾類型。
TextView期望綁定值是一個字串類型,通過調用setViewText(TextView, String)綁定。
ImageView期望綁定值是一個資源id或者一個字串,通過調用setViewImage(ImageView, int) 或 setViewImage(ImageView, String)。
如果沒有一個合適的綁定發生將會拋出IllegalStateException。
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
建構函式
參數
context 關聯SimpleAdapter運行著的視圖的上下文。
data 一個Map的列表。在列表中的每個條目對應列表中的一行,應該包含所有在from中指定的條目
resource 一個定義清單項目的視圖布局的資源唯一標識。布局檔案將至少應包含哪些在to中定義了的名稱。
from 一個將被添加到Map上關聯每一個項目的列名稱的列表
to 應該在參數from顯示列的視圖。這些應該全是TextView。在列表中最初的N視圖是從參數from中最初的N列擷取的值。
package android.imageview;
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.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
public class ImageView1Activity extends Activity {
/** Called when the activity is first created. */
private Spinner spinner1 = null;
private List<Map<String,Object>> list = null;
private SimpleAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = new ArrayList<Map<String,Object>>();
Map<String,Object> hashmap = new HashMap<String,Object>();
hashmap.put("itemsrc",R.drawable.macos01);
hashmap.put("itemname","資訊學院");
list.add(hashmap);
HashMap<String,Object> hashmap1 = new HashMap<String,Object>();
hashmap1.put("itemsrc",R.drawable.macos04);
hashmap1.put("itemname","信電學院");
list.add(hashmap1);
HashMap<String,Object> hashmap2 = new HashMap<String,Object>();
hashmap2.put("itemsrc",R.drawable.macos01);
hashmap2.put("itemname","經管學院");
list.add(hashmap2);
spinner1 = (Spinner)findViewById(R.id.spinner1);
spinner1.setPrompt("你要以後在哪工作?");
adapter = new SimpleAdapter(ImageView1Activity.this,list,R.layout.other,new String[]{"itemsrc","itemname"},new int[]{R.id.text1,R.id.text2});
spinner1.setAdapter(adapter);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Spinner"/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
other.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow >
<ImageView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
摘自 落日小屋