这是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。
一般用于ListActivity中的ListView
在ListView的布局当中作为
setListAdapter()的参数来填充List当中的内容
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
)。在列表中的每个条目对应列表中的一行,应该包含所有在from中指定的条目package com.example.imageview1activity; 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.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.ic_launcher); hashmap.put("itemname","信息学院"); list.add(hashmap); HashMap<String,Object> hashmap1 = new HashMap<String,Object>(); hashmap1.put("itemsrc",R.drawable.ic_launcher); hashmap1.put("itemname","信电学院"); list.add(hashmap1); HashMap<String,Object> hashmap2 = new HashMap<String,Object>(); hashmap2.put("itemsrc",R.drawable.ic_launcher); 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" android:spinnerMode="dialog"/> </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>