main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:text="TextView" android:id="@+id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
首先我们需要确实的是,ListView的布局也完成了,并通过调用
setContentView(…)进行了绑定,但直到
现在我们还没有确定
ListView中的第一行显示的格式是什么,是直接显示文字还是要“图文并茂”的显示。
Android系统为我们提供了多种模板进行选择
(android.R.layout)
,如
Ø
Simple_list_item_1 每项有一个
TextView
Ø
Simple_list_item_2
每项有两个
TextView
Ø
Simple_list_item_checked 带
CheckView的项
Ø
Simple_list_item_multiple_choise 每项有一个
TextView并可以多选
Ø
Simple_list_item_single_choice 每项有一个
TextView,但只能进行单选。
但然,如果以上项模板还无法满足你的要求,那只能自定义模板了(相当简单,就是定义一个
layout布局)。如果你做的
asp.net的开发的话,是否对
dataList控件有印象呢。如果对
DataList有印象,那么理解
ListView也就相当的简单了。
自定义模板可以根据自己的需要定义成任意的格式,包括图片、方案及其他可显示的
View,不用
多说,自己定义就好了,关键是如果使用并进行模板的绑定。
如何要对
ListView进行数据绑定,必须使用到一个接口:
Adapter。
其中最经常与
ListView进行配合使用的有
ArrayAdapter、
CursorAdapter及
SimpleAdapter等。
从名称可以看出
ArrayAdapter使用的是一个
ArrayAdapter做为数据源,
SimpleCursorAdapter使用的是一个
Cursor使用数据源,都比较容易理解,那么如何使用
SimpleAdapter作为数据的适配器呢
。
Ok,
从易到难。
ArrayAdapter:
String[] data = { "Item1", "Item2",
"Item3", "Item4", "Item5" };
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, data));
SimpleCursorAdapter:
//从数据库中查询
Cursor
cursor = adapter.getAllNotes();
startManagingCursor(cursor);
//设置要显示的数据源中的列名(需要包含在
cursor中)
String[] from = new String[] { DiaryDbAdapter.KEY_COLUMN_TITLE,
DiaryDbAdapter.KEY_COLUMN_CREATEED };
//显示的
View(
自定义模板中的
View)
int[] to = new int[] { R.id.txtRowTitle, R.id.txtRowCreateed };
//绑定
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout
.diaryrow, cursor, from, to);
setListAdapter(notes);、
SimpleAdapter:
SimpleAdapter将一个
List做为数据源,可以让
ListView进行更加个性化的显示。而
List中的第一项是个
Map<String,?>(用到泛型
),其中
Map中的每项将与
ListView中的每项进行一一对应绑定。
Ok,看一下构造:
SimpleAdapter(Context context,List<? Extends Map<String,?>> data,int resource,String [] form, int [] to);
Context:当前上下文,一般把
Activity.this传递进行。
Data:
数据源。
Resource: 自定义
的
layout模板资源,可以用
R.layout.xxx获取引用。
Form: 定义
ListView中的每一项数据索引,索引来自于
Map<String,?>
,即指定要显示的内容。
To:View数组,在
ListView模板中的定义
View,
与
Form中
需要一一对应。
事例代码:
List<Hashtable<String, Object>> listContent
= new
ArrayList<Hashtable<String, Object>>();
for
(int
i = 0; i < deviceList
.size(); i++) {
Hashtable<String, Object> table
= new
Hashtable<String, Object>();
table.put("name", deviceList
.get(i).Name);
table.put("address", deviceList
.get(i).Address);
table.put("type", deviceList
.get(i).Type + "");
listContent.add(table);
}
adapter
= new
SimpleAdapter(HeartActivity.this
,
listContent, R.layout.child
, //自定义的
layout
new
String[] { "name", "address" },
new
int
[] {R.id.txtDeviceName
, R.id.txtDeviceAddress
});
setListAdapter(adapter
);
以上代码使用了
Hashtable做为一个
Map,并添加到一个
List<Hashtable<String, Object>>当中。
之后
new一个
SimpleAdapter,注意
SimpleAdapter是如何生成的
。