SimpleAdapter

1、视图

1)主视图

 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     tools:context=".MainActivity" >
 6 
 7     <ListView 
 8          android:layout_width="match_parent"
 9          android:layout_height="match_parent"
10          android:id="@+id/lv"
11         ></ListView>
12 
13     
14 </RelativeLayout>

2)item视图

 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     <ImageView 
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:id="@+id/iv"
10         />
11     <TextView 
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:id="@+id/tv"
15         />
16 
17 </LinearLayout>

2、java代码

 1 package com.example.simpleadapter;
 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.os.Bundle;
 9 import android.app.Activity;
10 import android.view.Menu;
11 import android.widget.ListView;
12 import android.widget.SimpleAdapter;
13 
14 public class MainActivity extends Activity {
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         
21         ListView lv = (ListView)findViewById(R.id.lv);
22         
23         List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
24         Map<String,Object> map1 = new HashMap<String,Object>();
25         map1.put("nametext", "我是第一个功能");
26         map1.put("iconid", R.drawable.btn_minus_disable_focused);
27         
28         Map<String,Object> map2 = new HashMap<String,Object>();
29         map2.put("nametext", "我是第二个功能");
30         map2.put("iconid", R.drawable.btn_minus_selected);
31         
32         Map<String,Object> map3 = new HashMap<String,Object>();
33         map3.put("nametext", "我是第三个功能");
34         map3.put("iconid", R.drawable.btn_radio_off_selected);
35         
36         Map<String,Object> map4 = new HashMap<String,Object>();
37         map4.put("nametext", "我是第四个功能");
38         map4.put("iconid", R.drawable.btn_radio_on_focused_holo_dark);
39         
40         Map<String,Object> map5 = new HashMap<String,Object>();
41         map5.put("nametext", "我是第五个功能");
42         map5.put("iconid", R.drawable.btn_radio_on_holo_dark);
43         
44         data.add(map1);
45         data.add(map2);
46         data.add(map3);
47         data.add(map4);
48         data.add(map5);
49         
50         lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item, new String[]{"nametext","iconid"}, new int[]{R.id.tv,R.id.iv}));
51     }
52 
53 }

 

posted @ 2016-04-14 16:04  zhongyinghe  阅读(154)  评论(0编辑  收藏  举报