Android SimpleAdapter ListView (锁定手机,解锁手机的列表)

SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局。

构造方法:

 SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

  参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文

  参数data:Map列表,列表要显示的数据,这部分需要自己实现,类型要与上面的一致,每条项目要与from中指定条目一致

  参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id

  参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称

  参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应

首先来看效果图:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent" >
 4 
 5     <ListView
 6         android:id="@+id/lv"
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content" />
 9 
10 </RelativeLayout>
activity_main.xml
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent" >
 4 
 5     <ImageView
 6         android:id="@+id/left_img"
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:src="@drawable/img01" />
10 
11     <TextView
12         android:id="@+id/title"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:layout_marginLeft="20dp"
16         android:layout_marginTop="10dp"
17         android:layout_toRightOf="@id/left_img"
18         android:text="锁定手机"
19         android:textSize="24sp" />
20 
21     <ImageView
22         android:id="@+id/right_img"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:layout_alignParentRight="true"
26         android:layout_alignParentTop="true"
27         android:src="@drawable/ic_launcher" />
28     <TextView
29         android:id="@+id/content"
30         android:layout_below="@id/left_img"
31         android:layout_width="match_parent"
32         android:layout_marginTop="10dp"
33         android:layout_height="wrap_content"
34         android:singleLine="true"
35         android:text="简介键尖技术男女生就是就是计算机技术就是就是解决"/>
36 
37 </RelativeLayout>
item_layout
 1 public class MainActivity extends Activity {
 2 
 3     ListView lv;
 4 
 5     // 数据源
 6     int[] leftImgs = { R.drawable.img01, R.drawable.img02, R.drawable.img03,
 7             R.drawable.img04, R.drawable.img05, R.drawable.img06, };
 8     String[] titles = { "锁定手机", "手机杀毒", "解锁手机", "手机上网", "我的文档", "手机导航" };
 9     int rightImgs = R.drawable.ic_launcher;
10 
11     String content = "专门处理锁定手机相关功能,我是采用构造方法的守护神是计算机技术三角肌松解术世尊计上计";
12 
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17 
18         lv = (ListView) findViewById(R.id.lv);
19 
20         List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
21         for (int i = 0; i < 6; i++) {
22 
23             HashMap<String, Object> map = new HashMap<String, Object>();
24             map.put("leftImg", leftImgs[i]);
25             map.put("title", titles[i]);
26             map.put("rightImg", rightImgs);
27             map.put("content", content);
28 
29             data.add(map);
30         }
31 
32         SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data,
33                 R.layout.item_phone, new String[] { "leftImg", "title",
34                         "rightImg", "content" }, new int[] { R.id.left_img,
35                         R.id.title, R.id.right_img, R.id.content });
36         
37         lv.setAdapter(adapter);
38     }
39 
40 }
MainActivity.java
原文地址:https://www.cnblogs.com/Claire6649/p/5968759.html