android之自定义SimpleCursorAdapter的使用
来源:互联网 发布:protobuf.js 教程 编辑:程序博客网 时间:2024/04/28 11:16
SimpleCursorAdapter直接使用的方法:
SimpleCursorAdapter允许你绑定一个游标的列到ListView上,并使用自定义的layout显示每个项目。
SimpleCursorAdapter的创建,需要传入当前的上下文、一个layout资源,一个游标和两个数组:一个包含使用的列的名字,另一个(相同大小)数组包含View中的资源ID,用于显示相应列的数据值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//第一步:从数据库读取数据
dbHelper =
new
DBHelper(HistoryOrderActivity.
this
);
database = dbHelper.getWritableDatabase();
cursor = database.rawQuery(
"SELECT * FROM "
+DBHelper.TABLE_ORDER+
" where feedbackTime is not null"
,
null
);
//startManagingCursor(cursor); 被遗弃的方法,主要是把cursor的生命周期交由Activity管理
String[] fromColumns =
new
String[] {
"orderDescription"
,
"orderEffectiveTime"
,
"orderConsumeTime"
,
"promotion"
,
"feedbackInfo"
,
"feedbackTime"
,
};
int
[] toLayoutIDs =
new
int
[] {
R.id.description,
R.id.effectiveTime,
R.id.consumeTime,
R.id.promotion,
R.id.feedbackInfo,
R.id.feedbackTime};
adapter =
new
SimpleCursorAdapter(
this
, R.layout.histortyorder, cursor, fromColumns, toLayoutIDs,
0
);
SimpleCursorAdapter自定义的使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//第一步:从数据库读取数据
dbHelper =
new
DBHelper(HistoryOrderActivity.
this
);
database = dbHelper.getWritableDatabase();
cursor = database.rawQuery(
"SELECT * FROM "
+DBHelper.TABLE_ORDER+
" where feedbackTime is not null"
,
null
);
//startManagingCursor(cursor); 被遗弃的方法,主要是把cursor的生命周期交由Activity管理
String[] fromColumns =
new
String[] {
"orderDescription"
,
"orderEffectiveTime"
,
"orderConsumeTime"
,
"promotion"
,
"feedbackInfo"
,
"feedbackTime"
,
};
int
[] toLayoutIDs =
new
int
[] {
R.id.description,
R.id.effectiveTime,
R.id.consumeTime,
R.id.promotion,
R.id.feedbackInfo,
R.id.feedbackTime};
if
(cursor ==
null
) {
return
;
}
adapter =
new
HistoryOrderAdapter(HistoryOrderActivity.
this
, R.layout.histortyorder,
cursor, fromColumns, toLayoutIDs,
0
);
适配器的实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public
class
HistoryOrderAdapter
extends
SimpleCursorAdapter {
private
Cursor m_cursor;
private
Context m_context;
private
LayoutInflater miInflater;
public
HistoryOrderAdapter(Context context,
int
layout, Cursor c,
String[] from,
int
[] to,
int
flags) {
super
(context, layout, c, from, to, flags);
m_context = context;
m_cursor = c;
}
@Override
public
void
bindView(View arg0, Context arg1, Cursor arg2) {
View convertView =
null
;
if
(arg0 ==
null
) {
convertView = miInflater.inflate(R.layout.histortyorder,
null
);
}
else
{
convertView = arg0;
}
TextView tv_Description = (TextView) convertView
.findViewById(R.id.description);
TextView tv_EffectiveTime = (TextView) convertView
.findViewById(R.id.effectiveTime);
TextView tv_ConsumeTime = (TextView) convertView
.findViewById(R.id.consumeTime);
TextView tv_promotion = (TextView) convertView
.findViewById(R.id.promotion);
TextView tv_FeedbackInfo = (TextView) convertView
.findViewById(R.id.feedbackInfo);
TextView tv_FeedbackTime = (TextView) convertView
.findViewById(R.id.feedbackTime);
tv_Description.setText(arg2.getString(arg2
.getColumnIndex(
"orderDescription"
)));
tv_EffectiveTime.setText(ShopUtils.changeTimestampToTime(Long
.valueOf(arg2.getString(arg2
.getColumnIndex(
"orderEffectiveTime"
)))));
tv_ConsumeTime
.setText(ShopUtils.changeTimestampToTime(Long.valueOf(arg2
.getString(arg2.getColumnIndex(
"orderConsumeTime"
)))));
tv_promotion.setText(arg2.getString(arg2.getColumnIndex(
"promotion"
)));
tv_FeedbackInfo.setText(arg2.getString(arg2
.getColumnIndex(
"feedbackInfo"
)));
tv_FeedbackTime.setText(ShopUtils.changeTimestampToTime(Long
.valueOf(arg2.getString(arg2.getColumnIndex(
"feedbackTime"
)))));
}
}
HistoryOrderAdapter有点糙,需要改进。
0 0
- android之自定义SimpleCursorAdapter的使用
- Android开发模板------自定义SimpleCursorAdapter的使用
- Android SimpleCursorAdapter的使用
- Android中SimpleCursorAdapter的使用
- Android基础学习之BaseAdaptery以及SimpleCursorAdapter的使用
- Android中Adapter之SimpleCursorAdapter使用
- Android中Adapter之SimpleCursorAdapter使用
- android SQlite结合SimpleCursorAdapter的简单使用
- Android适配器之---SimpleCursorAdapter
- 我的Android进阶之旅------>Android ListView 应用解析(使用ArrayAdapter,SimpleAdapter和SimpleCursorAdapter适配器)
- Android的SimpleCursorAdapter出错
- SimpleCursorAdapter 的使用
- SimpleCursorAdapter的使用
- SimpleCursorAdapter的使用
- SimpleCursorAdapter的简单使用
- Android ListView 最基本的用法,使用SimpleCursorAdapter 。 附例子。
- Android ListView 最基本的用法,使用SimpleCursorAdapter
- Android 使用SimpleCursorAdapter为listview的item添加按钮
- Android 入门之 安装镜像 (鉴于国内Google相关很多服务都无法访问)
- winform 两个窗体之间来回传值/相互传值
- 移动单元格
- mac 开发工具
- DRM in Android
- android之自定义SimpleCursorAdapter的使用
- Android之Service
- OpenStack部署工具总结
- svn不能添加.a文件的解决方法
- C/C++编译过程
- Spring AMQP异常处理和事物
- Qt Model/View 学习笔记 (四)
- 码农演奏的吉他录音小样
- Actionbar的使用