本例把SimpleCursorAdapter做了一下扩展。每一项item可由layout中listitem.xml来控制布局。数据可由sqlite中取出。 <本文为原创文章,转载请注明出处。>
1.上主代码:
01
package
com.CursorAdapterTest;
03
import
static
com.db.MyOpenHelper.*;
05
import
com.db.MyOpenHelper;
06
import
android.app.Activity;
07
import
android.content.ContentValues;
11
package
com.CursorAdapterTest;
12
import
android.widget.TextView;
14
public
class
CursorAdapterTest
extends
Activity {
16
/** Called when the activity is first created. */
17
MyOpenHelper myHelper ;
22
private
static
SQLiteDatabase db =
null
;
25
public
void
onCreate(Bundle savedInstanceState) {
26
super
.onCreate(savedInstanceState);
27
setContentView(R.layout.main);
29
lv = (ListView)findViewById(R.id.lv);
32
myHelper =
new
MyOpenHelper(
this
, DB_NAME);
33
db = myHelper.getWritableDatabase();
42
Cursor c = db.query(TABLE_NAME,
null
,
null
,
null
,
null
,
null
, ID);
43
startManagingCursor(c);
46
Adapter adapter =
new
Adapter(
this
,
49
new
String[]{ID,NAME,PHONE},
50
new
int
[]{R.id.list_Id,R.id.list_Name,R.id.list_Phone});
52
lv.setAdapter(adapter);
54
public
class
Adapter
extends
SimpleCursorAdapter {
56
private
LayoutInflater mInflater;
58
public
Adapter (Context context,
int
layout, Cursor c, String[] from,
int
[] to) {
59
super
(context, layout, c, from, to);
63
public
void
bindView(View view, Context context, Cursor cursor) {
65
LinearLayout ll =
null
;
67
ll = (LinearLayout) mInflater.inflate(R.layout.listitem,
null
);
69
ll = (LinearLayout)view;
73
int
idCol = cursor.getColumnIndex(ID);
74
int
nameCol = cursor.getColumnIndex(NAME);
75
int
phoneCol = cursor.getColumnIndex(PHONE);
77
int
nCount = cursor.getPosition();
81
ImageView iv_TView = (ImageView)ll.findViewById(R.id.list_Iv);
82
TextView id_TV = (TextView)ll.findViewById(R.id.list_Id);
83
TextView name_TV = (TextView)ll.findViewById(R.id.list_Name);
84
TextView phone_TV = (TextView)ll.findViewById(R.id.list_Phone);
88
iv_TView.setImageDrawable(getResources().getDrawable(R.drawable.r));
90
iv_TView.setImageDrawable(getResources().getDrawable(R.drawable.b));
92
id_TV.setText(cursor.getString(idCol));
93
name_TV.setText(cursor.getString(nameCol));
94
phone_TV.setText(cursor.getString(phoneCol));
main.xml布局文件
3
<
listview
android:id
=
"@+id/lv"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:dividerheight
=
"1px"
android:cachecolorhint
=
"#00000000"
android:smoothscrollbar
=
"true"
>
每一个item的布局文件listitem.xml
3
<
imageview
android:id
=
"@+id/list_Iv"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_gravity
=
"center_vertical"
>
4
<
linearlayout
android:orientation
=
"vertical"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_weight
=
"1.0"
>
5
<
textview
android:id
=
"@+id/list_Id"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
>
6
<
textview
android:id
=
"@+id/list_Name"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
>
7
<
textview
android:id
=
"@+id/list_Phone"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
>
8
</
textview
></
textview
></
textview
></
linearlayout
>
9
</
imageview
></
linearlayout
>
2.主代码分析 1>首先得到ListView
1
lv = (ListView)findViewById(R.id.lv);
2>打开数据库并得到游标
2
myHelper =
new
MyOpenHelper(
this
, DB_NAME);
3
db = myHelper.getWritableDatabase();
5
Cursor c = db.query(TABLE_NAME,
null
,
null
,
null
,
null
,
null
, ID);
6
startManagingCursor(c);
3>实例化由SimpleCursorAdpter继承出来的Adapter并给listview对象绑定此适配器
2
Adapter adapter =
new
Adapter(
this
,
5
new
String[]{ID,NAME,PHONE},
6
new
int
[]{R.id.list_Id,R.id.list_Name,R.id.list_Phone});
8
lv.setAdapter(adapter);
4>下面来看一下由SimpleCursorAdapter继承/定制出来的Adapter 在定制出来的Adapter中,除了一些必要的构造函数之外,我们只需要重写bindView()即可(newView()就不用写了)。 看一下官方文档中给出的bindView()函数的解释: bindView(View view, Context context, Cursor cursor) Binds all of the field names passed into the "to" parameter of the constructor with their corresponding cursor columns as specified in the "from" parameter.
就是说bindView()是通过SimpleCursorAdapter的构造函数传进来的context(listview要显示在的activity)、每一个item的布局文件(listitem.xml)、要绑定的数据以及每一个数据的显示方式(在listitem中已定义),并且通过cursor把每一个item绑定(bind)到每一个view上。并且在bindView()中我们还可以绑定不在数据库中的数据,比如说图片、按钮等等。 这就是使用bindView()和不使用bindView()的好处之一。其他优点大家可以参阅相关文档。