Android中的列表,当然也可以用ListView来完成所需要的功能,用法是一样的。

废话不说,来关键的。

LiveActivity本身继承了关于List操作的众多接口,我们可以方便的重写这些操作中需要的方法来实现自己需要的功能。

如果要用ListActivity,则ActivityLayout文件中必须包括一个(只能一个)ListView,且ListViewid="@id/android:list"

如下代码,一个标准的ListActivity Layout文件:

<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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">

<ListViewandroid:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>

<TextViewid="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>

请注意 ListView与TextView的id。前面说了,

1. ListView的Id为固定不变的,为"@id/android:list“,ListActivity会根据id自动查找ListView引用;在 Activity 中使用 setListAdapter(adapter); 时就默认设置到了这个list上。如果按一般控件的写法 <ListView android:id="@+id/myListView" …… />,则需要 findViewById 先得到控件对像,再调用对像的 setListAdapter(adapter);

2. 但如果当ListView中没有值而又想提示一句话时,那么用于指定显示提示信息的TextView的id必须为”"@id/android:empty",提示的信息可以通过android:text进行指定。

在实际中,也可以这样写,如Android2.3中的call_detail.xml中:

<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
/>

<ScrollView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<TextView android:id="@+id/emptyText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/unknown"
android:textSize="20sp"
android:textColor="?android:attr/textColorSecondary"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:gravity="center"
android:lineSpacingMultiplier="0.92"/>

</ScrollView>

这个写法更加实用了,可以通过在listview没有显示数据时可以用@+id/emptyText进行动态赋值,而不用像前一个例子中的将empty text固化到xml中

OK,关于如何布局说完了,那么如何给List绑定值,并进行操作呢?

首先我们需要确实的是,ListView的布局也完成了,并通过调用setContentView(…)进行了绑定,但直到现在我们还没有确定ListView中的第一行显示的格式是什么,是直接显示文字还是要“图文并茂”的显示。

Android系统为我们提供了多种模板进行选择(android.R.layout),如

ØSimple_list_item_1每项有一个TextView

ØSimple_list_item_2每项有两个TextView

ØSimple_list_item_checkedCheckView的项

ØSimple_list_item_multiple_choise每项有一个TextView并可以多选

ØSimple_list_item_single_choice每项有一个TextView,但只能进行单选。

但然,如果以上项模板还无法满足你的要求,那只能自定义模板了(相当简单,就是定义一个layout布局)。如果你做的asp.net的开发的话,是否对dataList控件有印象呢。如果对DataList有印象,那么理解ListView也就相当的简单了。

自定义模板可以根据自己的需要定义成任意的格式,包括图片、方案及其他可显示的View,不用多说,自己定义就好了,关键是如果使用并进行模板的绑定。

如何要对ListView进行数据绑定,必须使用到一个接口:Adapter

其中最经常与ListView进行配合使用的有ArrayAdapterCursorAdapterSimpleAdapter等。

从名称可以看出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

=newArrayList<Hashtable<String, Object>>();

for(inti = 0; i <deviceList.size(); i++) {

Hashtable<String, Object> table

=newHashtable<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=newSimpleAdapter(HeartActivity.this,

listContent, R.layout.child, //自定义的layout

newString[] {"name","address"},

newint[] {R.id.txtDeviceName, R.id.txtDeviceAddress});

setListAdapter(adapter);

以上代码使用了Hashtable做为一个Map,并添加到一个List<Hashtable<String, Object>>当中。

之后new一个SimpleAdapter,注意SimpleAdapter是如何生成的。

更多相关文章

  1. Android应用程序绑定服务(bindService)的过程源代码分析(3)
  2. Android:为控件绑定监听器
  3. 告别Dagger2模板代码:DaggerAndroid原理解析
  4. 告别Dagger2模板代码:DaggerAndroid使用详解
  5. android ListView中添加ImageButton按钮并绑定事件
  6. TabLayout绑定Viewpager后不显示文字
  7. Android Button 控件绑定单击事件

随机推荐

  1. Android平台特性
  2. Android(安卓)4.4 meminfo 实现分析
  3. Android应用程序与SurfaceFlinger服务的
  4. android源代码的利用
  5. [原创]Android中LocationManager的简单使
  6. Android移植到PXA270开发板
  7. 【Google Voice】Android(安卓)轻松实现
  8. [Android] 内存泄漏调试经验分享 (一)
  9. Android系统构架简介
  10. Android中使用硬盘模拟SD卡