android之ListView和SimpleAdapter的组合使用
SimpleAdapter是简单适配器。可以将准备好的数据显示在ListView中。更多信息可以看看Android 官方API开发文档。
示例说明:在SimpleAdapterListViewActivity.java中的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package com.zzh.day_listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
/**SimpleAdapter在ListView中的使用 * @author Administrator
*
*/
public class SimpleAdapterListViewActivity extends Activity
{ SimpleAdapter adapter;
ListView lv;
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); //要显示的数据
Map<String, Object> map; //
@Override
protected void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.simple_listview);
lv = (ListView) findViewById(R.id.listView1);
// 初始化数据
for ( int i = 1 ; i < 21 ; i++)
{
map = new HashMap<String, Object>();
map.put( "key1" , i + " <--key1" );
map.put( "key2" , i+ " <--key2" );
data.add(map);
}
/*
* SimpleAdapter构造方法中参数的意义:
* 参数一:要显示在的activity
* 参数二:要显示的数据。此数据必须是List<? extends Map<String, ?>> 类型的,而且Map里的键必须是String类型的。
* 参数三:自定义的布局文件。定义了两个TextView,因为我只在初始化数据的时候放了两个map对象。
* 参数四:是一个String类型的数组,里面放的是,参数二中Map 键的数组,而且必须是,这也说明了Map里的键必须是String类型的原因
* 参数五:int类型的数组,存放的是,参数二中Map中存的值所要放的位置(View)的id。
* 在这里可以将一个map集合看成是一个ListView中的一个item,即一行数据,而key则代表着每个View,value则是要显示的数据。
* */
adapter = new SimpleAdapter( this , data, R.layout.simple_listview_item,
new String[] { "key1" , "key2" }, new int [] { R.id.textView1, R.id.textView2 });
lv.setAdapter(adapter); //将数据显示在ListView中
lv.setOnItemClickListener( new OnItemClickListener() //给ListView注册事件
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Toast.makeText(getApplicationContext(), "点击了第" +position+ "项" , Toast.LENGTH_LONG).show();
}
});
}
} |
1
|
simple_listview.xml中的内容: |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical" >
< ListView
android:id = "@+id/listView1"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" >
</ ListView >
</ RelativeLayout >
|
simple_listview_item.xml中的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout 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/textView1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentLeft = "true"
android:layout_alignParentTop = "true"
android:textSize = "26sp" />
< TextView
android:id = "@+id/textView2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentRight = "true"
android:layout_alignParentTop = "true"
android:textSize = "26sp"
android:textColor = "@android:color/holo_blue_light" />
</ RelativeLayout >
|
运行效果:
本文转自 墨宇hz 51CTO博客,原文链接:http://blog.51cto.com/zzhhz/1631765