浩哥,只能按我的理解来说说了
ListActivity ListView Adapter
ListActivity 逻辑控制,ListView 数据展示,Adapter 数据源
详细说明请看这里:
Adapter部分:
http://code.google.com/p/androidlearn/wiki/Adapter
自己继承Adapter
http://code.google.com/p/androidlearn/wiki/Adapter_custom
而ListView 是 AdapterView的子类,
AdapterView.OnItemClickListener、AdapterView.OnItemSelectedListener 这些关乎Item的操作都是在AdapterView中就有的抽象。
ListView 只是在此基础上加了一些其他的功能,比如:
addFooterView、addHeaderView 等,
这些方法的用处在我以前给你的k-9源码中用到,其实就加了个特殊性的Item而且还是在头和脚加特殊的,中间的加不了。
如果认真看API差不多就这个东西,除了那些get或boolean的方法。
Adapter 的数据源有三种
- ArrayAdapter
- ListAdapter
- SimpleCursorAdapter
这三个的区别,浩哥针对上面三种名字可以看出,
第一个ArrayAdapter是数组的一条条数据显示在listview
第二个ListAdapter 我就不说了看前面这个List就知道什么意思了呵呵
CursorAdapter、SimpleCursorAdapter都是关乎数据库的一条条记录显示在ListView
下面这句话情调了Cursor和ListView对应必须要"_id"这个字段,不然不能工作。
Adapter that exposes data from aCursor ListView widget. The Cursor must include a column named "_id" or this class will SimpleCursorAdapter[/url]是一层层继承而来,最初它继承了CursorAdapter、而CursorAdapter是继承BaseAdapter的,
在BaseAdapter中有个getView方法是动态定义布局的,所以Layout的布局定义可以在后来的继承BaseAdapter的类中自己去实现,
所以你自己也可以写个MySimpleCursorAdapter[/url]来实现这个getView的布局,这个时候你可以根据cursor的字段值来设置属性在getView中
在getView中通过Cursor mCursor = (Cursor) getItem(position);来得到一条条Item所对应数据库每一行的cursor。
在ListActivity 中特别提到了关乎布局的东西
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android
rientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView
android:id="@id/android:list" //这个写法有点特殊吧呵呵
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView
id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
上面只是定义了listveiw的布局,你还要定义Item的布局,那就是一般的布局,比如下面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android
rientation="vertical">
<TextView
android:id="@+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
这个Item的布局是对应的比如在代码中:Cursor cursor = managedQuery(getIntent().getData(), PROJECTION,
"folder = ?", new String[] { folder },SimpleCursorAdapter adapter = new MySimpleCursorAdapter(this,
R.layout.message_list_item, cursor, new String[] {
MessageInfoHolder.MimeMessageColumns.SUBJECT,
MessageInfoHolder.MimeMessageColumns.FROM,
MessageInfoHolder.MimeMessageColumns.CREATED },
new int[] { R.id.subject, R.id.from, R.id.date });
通过 ListActivity.setAdapter(adapter);方法把这个数据源就加载进来了。
ListActivity的get和set的public不算外只是多了一个方法onContentChanged()当内容改变的方法。从这个可以看出ListActivity和一般的Activity没什么区别,区别只在于提供了ListView的一些xml布局的要求,和怎么获取[ListView[/url]和怎么获取ListAdapter[/url]以及怎么set这两个东西一个加布局一个加数据,那ListActivity只是个桥路而已了。

1894

被折叠的 条评论
为什么被折叠?



