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>



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值