Adapter(三)——SimpleAdapter

    SimpleAdapter相对于ArrayAdapter来说更为复杂,因为其每一个列表项中含有不同的子控件。

对比ArrayAdapter:

1、数据源发生变化,更加的复杂。

2、可以支持多控件的适配。

 

代码示例:

MainActivity.java

 

private ListView lv;
private SimpleAdapter adapter;
private List<Map<String,Object>> list;
private Map<String,Object> map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //初始化ListView
    lv = (ListView) findViewById(R.id.lv);
    list = new ArrayList<Map<String,Object>>();
    for (int i = 0; i < 40 ; i++){
        map = new HashMap<String,Object>();
        map.put("data","数据" + i);
        map.put("context","内容" + i);
        list.add(map);
    }
    // 参数一:上下文对象;
    // 参数二:数据源。List<Map<String,Object>【List集合里面是Map对象,Map对象的key是String】
    // 参数三:item对应的布局文件;
    // 参数四:表示由map中定义的key组成的字符串类型的数组
    // 参数五:需要显示的控件id组成的int类型的数组
    // 必须保证参数四和参数五一一对应,否则会导致错误显示。
    String[] from = {"data","context"};
    int[] to = {R.id.data,R.id.context};
    adapter = new SimpleAdapter(this,list,R.layout.text_simpleadapter_item,from,to);
    //设置适配器进行显示
    lv.setAdapter(adapter);

 

 

在初始化中我们new

 SimpleAdapter(Context context,List<Map<String,Object>> data,int resource,String[] from,int[] to)方法:

参数一:上下文对象。

参数二:数据源,类型为List<Map<String,Object>>【List集合里面是Map对象,Map对象的key值为String】。

参数三:item对应的布局文件。

参数四:表示由map中定义的key组成的字符串类型的数组。

参数五:需要显示的控件id组成的int类型数组。

注意:参数四与参数五必须一一对应。

 

text_simpleadapter_item.xml

 

<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/data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textSize="20sp"
        android:text="数据0"/>
    <TextView
        android:id="@+id/context"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textSize="28sp"
        android:text="内容"/>

</LinearLayout>

上面是对SimpleAdapter的方法的熟悉,在对其熟悉之后我们可不可以用它来实现图文混排呢?接下来我们就去试试。

代码示例:

 

private ListView lv;
private SimpleAdapter adapter;
private List<Map<String,Object>> list;
private Map<String,Object> map;
private int[] images = {R.mipmap.ic_launcher,R.mipmap.ic_launcher,
        R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,
        R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
private String[] names = {"照相机","浏览器","通信录","便签","地图","音乐","备忘录","设置",
        "书架","主题"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //初始化控件
    lv = (ListView) findViewById(R.id.lv);
    //数据源进行初始化
    list = new ArrayList<Map<String,Object>>();
    for (int i = 0;i < names.length;i++){
        map = new HashMap<String,Object>();
        map.put("img",images[i]);//放入了图片的id
        map.put("name",names[i]);//放置上方的TextView显示的内容
        map.put("context",names[i]);//放置下方的TextView显示的内容
        list.add(map);
    }
    //初始化adapter
    String[] from = {"img","name","context"};
    int[] to = {R.id.pic_image,R.id.pic_name,R.id.pic_context};
    adapter = new SimpleAdapter(this,list,R.layout.pic_adapter_item,from,to);
    //设置适配器
    lv.setAdapter(adapter);
}

pic_adapter_item.xml

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp"
    android:layout_marginTop="10dp">

    <ImageView
        android:id="@+id/pic_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:id="@+id/pic_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/pic_image"
        android:textSize="18sp"
        android:layout_marginLeft="10dp"
        android:text="照相机"/>
    <TextView
        android:id="@+id/pic_context"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/pic_image"
        android:layout_below="@+id/pic_name"
        android:layout_alignLeft="@+id/pic_name"
        android:layout_alignBottom="@+id/pic_image"
        android:textSize="16sp"
        android:text="这是一个不错的照相机"/>
</RelativeLayout>

 

在这里大家一定要细心,因为AS自动填充功能会令你出现错误Resource ID #0x7f0b0054 type #0x12 is not valid。

 

adapter = new SimpleAdapter(this,list,R.layout.pic_adapter_item,from,to);

这里是R.layout.pic_adapter_item,而不是AS自动填充的R.id.pic_adapter_item,小心掉坑。

运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值