SimpleAdapter的使用

      SimpleAdapter是一个简单的适配器,该适配器也继承了BaseAdapter,对于布局是固定而言,使用简单适配器开发时非常简单了,由于SimpleAdapter适配器的布局是固定,比不上BaseAdapter灵活,比如说使用BaseAdapter适配器可以很容易控制不同的布局,BaseAdapter适用于多变的布局.

 

下面我们一起来看看SimpleAdapter适配的使用

 

在activity_main.xml布局文件中加入一个ListView控件

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.simpleadaptertest.MainActivity" >
10 
11     <ListView
12         android:id="@+id/listView1"
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:layout_alignParentLeft="true"
16         android:layout_alignParentTop="true"
17         android:layout_marginLeft="15dp" >
18     </ListView>
19 
20 </RelativeLayout>

 

编写一个Item.xml文件,该文件是ListView控件中的一个条目

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" >
 6 
 7     <ImageView
 8         android:id="@+id/img"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content" />
11 
12     <TextView
13         android:id="@+id/textview"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_weight="1" />
17 
18 </LinearLayout>

 

编写核心代码

 1 package com.example.simpleadaptertest;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import android.app.Activity;
 9 import android.os.Bundle;
10 import android.widget.ListView;
11 import android.widget.SimpleAdapter;
12 
13 public class MainActivity extends Activity {
14 
15     private List<Map<String, Object>> data;
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21 
22         data = new ArrayList<Map<String, Object>>();
23 
24         for (int i = 0; i < 100; i++) {
25             // 这是listView中的每一个条目
26             Map<String, Object> item = new HashMap<String, Object>();
27             item.put("img", R.drawable.ic_launcher);
28             item.put("name", "张三" + i);
29             //把数据添加到List集合中,也就是ListView中
30             data.add(item);
31         }
32 
33         // 获取ListView控件
34         ListView listView = (ListView) findViewById(R.id.listView1);
35 
36         // 创建一个SimpleAdapter
37         /**
38          * SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
39          * int resource, String[] from, int[] to)
40          * 
41          *  context 上下为对象
42          *  data 要在ListView中显示的数据
43          *  resource 把一个布局文件转换为一个View
44          *  from Map集合的Key    (准备要写入控件的数据)
45          *  to   布局文件中控件的id (把数据写入到这个控件中)
46          */
47         SimpleAdapter sa = new SimpleAdapter(MainActivity.this, data,
48                 R.layout.item,
49                 new String[]{"img","name"}, 
50                 new int[]{R.id.img,R.id.textview});
51         
52         //使用适配器
53         listView.setAdapter(sa);
54     }
56 }

远行结果: