当前位置:操作系统 > 安卓/Android >>

Android学习笔记之SimpleAdapter

这是一个简单的适配器,可以将静态数据映射到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> 
  \
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,