通过定制SimpleCursorAdapter来实现ListView
来源:互联网 发布:王祖贤 倩女幽魂 知乎 编辑:程序博客网 时间:2024/05/22 06:18
本例把SimpleCursorAdapter做了一下扩展。每一项item可由layout中listitem.xml来控制布局。数据可由sqlite中取出。
<本文为原创文章,转载请注明出处。>
1.上主代码:
01
package
com.CursorAdapterTest;
02
03
import
static
com.db.MyOpenHelper.*;
04
05
import
com.db.MyOpenHelper;
06
import
android.app.Activity;
07
import
android.content.ContentValues;
08
//...
09
//...
10
//省略部分代码
11
package
com.CursorAdapterTest;
12
import
android.widget.TextView;
13
14
public
class
CursorAdapterTest
extends
Activity {
15
16
/** Called when the activity is first created. */
17
MyOpenHelper myHelper ;
//声明MyOpenHelper对象
18
int
[] testId ;
//声明用于存放联系人id的数组
19
String [] testName ;
//声明用于存放联系人姓名的数组
20
String [] testPhone ;
//声明用于存放联系人电话的数组
21
ListView lv;
//声明ListView对象
22
private
static
SQLiteDatabase db =
null
;
23
24
@Override
25
public
void
onCreate(Bundle savedInstanceState) {
26
super
.onCreate(savedInstanceState);
27
setContentView(R.layout.main);
28
//得到ListView的布局
29
lv = (ListView)findViewById(R.id.lv);
30
31
//打开数据库
32
myHelper =
new
MyOpenHelper(
this
, DB_NAME);
33
db = myHelper.getWritableDatabase();
34
/*ContentValues values = new ContentValues();
35
36
for(int i=0;i<100;i++){
37
values.put(NAME,"A");
38
values.put(PHONE,1234567+i);
39
db.insert(TABLE_NAME, null, values);
40
}*/
41
//得到数据库的cursor
42
Cursor c = db.query(TABLE_NAME,
null
,
null
,
null
,
null
,
null
, ID);
43
startManagingCursor(c);
44
45
//实例化adapter
46
Adapter adapter =
new
Adapter(
this
,
47
R.layout.listitem,
48
c,
49
new
String[]{ID,NAME,PHONE},
50
new
int
[]{R.id.list_Id,R.id.list_Name,R.id.list_Phone});
51
//绑定adapter
52
lv.setAdapter(adapter);
53
}
54
public
class
Adapter
extends
SimpleCursorAdapter {
55
56
private
LayoutInflater mInflater;
57
58
public
Adapter (Context context,
int
layout, Cursor c, String[] from,
int
[] to) {
59
super
(context, layout, c, from, to);
60
}
61
62
@Override
63
public
void
bindView(View view, Context context, Cursor cursor) {
64
//得到每一个item的布局文件
65
LinearLayout ll =
null
;
66
if
(view ==
null
) {
67
ll = (LinearLayout) mInflater.inflate(R.layout.listitem,
null
);
68
}
else
{
69
ll = (LinearLayout)view;
70
}
71
72
//通过cursor得到每一个字段的ColumnIndex
73
int
idCol = cursor.getColumnIndex(ID);
74
int
nameCol = cursor.getColumnIndex(NAME);
75
int
phoneCol = cursor.getColumnIndex(PHONE);
76
77
int
nCount = cursor.getPosition();
78
//int nCount = Integer.valueOf(cursor.getString(idCol));
79
80
//得到每一个数据控件
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);
85
86
//设置每一项数据内容
87
if
( nCount %
2
==
0
){
88
iv_TView.setImageDrawable(getResources().getDrawable(R.drawable.r));
89
}
else
{
90
iv_TView.setImageDrawable(getResources().getDrawable(R.drawable.b));
91
}
92
id_TV.setText(cursor.getString(idCol));
93
name_TV.setText(cursor.getString(nameCol));
94
phone_TV.setText(cursor.getString(phoneCol));
95
}
96
}
97
}
main.xml布局文件
1
<!--?xml version="1.0" encoding="utf-8"?-->
2
<
linearlayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
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"
>
4
</
listview
>
5
</
linearlayout
>
每一个item的布局文件listitem.xml
1
<!--?xml version="1.0" encoding="utf-8"?-->
2
<
linearlayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
>
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>打开数据库并得到游标
1
//打开数据库
2
myHelper =
new
MyOpenHelper(
this
, DB_NAME);
3
db = myHelper.getWritableDatabase();
4
//得到游标
5
Cursor c = db.query(TABLE_NAME,
null
,
null
,
null
,
null
,
null
, ID);
6
startManagingCursor(c);
3>实例化由SimpleCursorAdpter继承出来的Adapter并给listview对象绑定此适配器
1
//实例化Adapter
2
Adapter adapter =
new
Adapter(
this
,
3
R.layout.listitem,
4
c,
5
new
String[]{ID,NAME,PHONE},
6
new
int
[]{R.id.list_Id,R.id.list_Name,R.id.list_Phone});
7
//绑定adapter
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()的好处之一。其他优点大家可以参阅相关文档。
- 通过定制SimpleCursorAdapter来实现ListView
- ListView(SimpleCursorAdapter实现)
- Android入门:ListView(SimpleCursorAdapter实现)
- 【android】ListView+SimpleCursorAdapter+checkbox实现批量删除
- android]ListView+SimpleCursorAdapter+checkbox实现批量删除
- ListView与SimpleCursorAdapter实现视频列表
- 继承SimpleCursorAdapter定制Adapter
- 采用ListView实现数据列表显示——SimpleCursorAdapter
- android ListView,SimpleCursorAdapter
- 通过Activity设置动态Fragment中的ListView(通过SimpleCursorAdapter适配)
- android ListView使用的三种方法汇总(通过继承BaseAdapter,使用SimpleAdapter和SimpleCursorAdapter)
- jqm通过指定class来定制样式
- SimpleCursorAdapter与ListView的使用
- ListView之SimpleCursorAdapter列表--3
- ListView适配器--ArrayAdapter;SimpleAdapter;SimpleCursorAdapter
- simpleCursorAdapter中的listview获取内容
- 使用SimpleCursorAdapter绑定显示listview
- 仿SimpleCursorAdapter实现的listview检索,同时支持局部高亮
- 设置和读取Cookie
- 22---数据库编程07(preparedStatement)
- 5种方便廉价的健身工具
- 云:一体无法万用
- Timer 定时器
- 通过定制SimpleCursorAdapter来实现ListView
- 22---数据库编程08(Clob)
- json转换为对象,对象转换为json flexjson的灵活运用
- 在Asp.net页面中调用颜色
- sql语句写法注意项
- spring-ws No adapter for endpoint ***: Is your endpoint annotated with @Endpoint?
- 第四周任务二:利用多文件组织项目
- 设置QLabel中字体的颜色
- c++中何时该用“引用传递”,何时该用“值传递”