ListView之SimpleAdapter

SimpleAdapter是安卓内置的适配器,本文展示的是listview的子项为{图片,文件}组合

如下图所示:

 

具体代码:

SimpleAdapter_test.java

 1 /*
 2 ListView :列表
 3 通常有两个职责:
 4 a.将数据填充到布局
 5 b.处理点击事件
 6 
 7 一个ListView创建需要几个元素:
 8 a.ListView中第一列的    View
 9 b.填入View的图片或数据
10 c.连接数据 与ListView的适配器
11 
12 
13 有哪些适配器?
14 ArrayAdapter<T>  用来绑定一个数组,支持泛型设计
15 SimpleAdapter 用来绑定在xml中定义的控件和对应的数据
16 SimpleCursorAdapter:用来绑定游标得到的数据
17 BaseAdapter 通用的基础适配器
18 
19 
20 
21  * 
22  * */
23 public class SimpleAdapter_test extends Activity {
24 
25     private ListView listview;
26     private int[] ids=new int[]{
27             R.drawable.s1,
28             R.drawable.s2,
29             R.drawable.s3,
30             R.drawable.s4,
31             R.drawable.s5};
32     
33     private SimpleAdapter adapter;
34     private Context context;
35     private List<Map<String,Object>> datas;
36     @Override
37     protected void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39         setContentView(R.layout.baseadapate);
40         context = this;
41         listview = (ListView) findViewById(R.id.listview);
42     
43         initData();
44         //map中所有的key的
45         String[] from=new String[]{"map_image","map_content"};
46         int[] to=new int[]{R.id.image,R.id.content};
47         adapter=new SimpleAdapter(context, datas, R.layout.items2, from, to);
48      
49         listview.setAdapter(adapter);
50         
51         listview.setOnItemClickListener(new OnItemClickListener() {
52 
53             @Override
54             public void onItemClick(AdapterView<?> parent, View view,
55                     int position, long id) {
56                 
57                 Toast.makeText(context,"你选中的是:"+ datas.get(position).get("map_content"), 0).show();
58             }
59         });
60         
61     }
62 
63 
64     private void initData() {
65         
66         datas = new ArrayList<Map<String,Object>>();
67         for(int i=0;i<5;i++)
68         {
69             Map<String,Object> map = new HashMap<String, Object>();
70             map.put("map_image",BitmapFactory.decodeResource(getResources(), ids[i]));
71             map.put("map_content", "hahacontent"+i);
72             datas.add(map);
73         }
74     }
75     
76 }

baseadapate.xml

 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="vertical" >
 6     
 7     <ListView 
 8         android:layout_width="match_parent"
 9         android:layout_height="match_parent"
10         android:id="@+id/listview"
11         >
12         
13     </ListView>
14 
15 </LinearLayout>

items2.xml

 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     android:layout_gravity="center"
 7     
 8     >
 9 
10     <ImageView
11         android:id="@+id/image"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content" 
14         android:background="@drawable/ic_launcher"
15         />
16 
17     <TextView
18         android:layout_marginTop="15dp"
19         android:id="@+id/content"
20         android:layout_width="0px"
21         android:layout_height="wrap_content"
22         android:layout_weight="1"
23         android:text="haa" 
24         
25         />
26 
27 </LinearLayout>


不要忘了在清单里注册activity,并且设置为app入口

1 <activity android:name=".BaseAdapter_test">
2             <intent-filter>
3                 <action android:name="android.intent.action.MAIN" />
4 
5                 <category android:name="android.intent.category.LAUNCHER" />
6             </intent-filter>
7 </activity>
原文地址:https://www.cnblogs.com/UniqueColor/p/5254220.html