参考文档:
http://www.it165.net/pro/html/201308/6827.html
http://stackoverflow.com/questions/12223293/cursoradapter-bindview-optimization
ListView数据来自数据库
CursorAdapter 继承了 BaseAdapter
好处:仅加载需要显示的数据,性能好
使用方法:
1.实现两个参数构造方法
2.重写newView()方法
layout->view
3.重写bindView()方法
view.set
数据更新:
adapter.changeCursor(cursor);
adapter.notifyDataSetChanged()
代码如下:
01.
public
class
MySqliteOpenhelper
extends
SQLiteOpenHelper
02.
{
03.
04.
public
MySqliteOpenhelper(Context context,
int
version)
05.
{
06.
super
(context,
"dianhuaben.db"
,
null
, version);
07.
}
08.
@Override
09.
public
void
onCreate(SQLiteDatabase db)
10.
{
//注意:使用CursorAdapter时,创建表必须有以_id为列名的列
11.
String sql =
"CREATE TABLE dhb (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))"
;
12.
db.execSQL(sql);
13.
}
14.
@Override
15.
public
void
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion)
16.
{
17.
}
18.
}
使用CursorAdapter的代码如下:
01.
public
void
createCursorAdapter(Cursor cursor)
02.
{
//游标适配器,构造方法,传入cursor
03.
mAdapter =
new
CursorAdapter(
this
, cursor)
04.
{
//重写两个方法
05.
@Override
06.
public
View newView(Context context, Cursor cursor, ViewGroup parent)
07.
{
//找到布局和控件
08.
ViewHolder holder =
new
ViewHolder();
09.
LayoutInflater inflater = getLayoutInflater();
10.
View inflate = inflater.inflate(R.layout.listview_item,
null
);
11.
holder.item_tv_name = (TextView) inflate.findViewById(R.id.item_tv_name);
12.
holder.item_tv_phone = (TextView) inflate.findViewById(R.id.item_tv_phone);
13.
inflate.setTag(holder);
14.
return
inflate;
//返回的view传给bindView。
15.
}
16.
17.
@Override
18.
public
void
bindView(View view, Context context, Cursor cursor)
19.
{
//复用布局。www.it165.net
20.
// 把数据设置到界面上
21.
ViewHolder holder = (ViewHolder) view.getTag();
22.
String name = cursor.getString(cursor.getColumnIndex(
"name"
));
23.
String phone = cursor.getString(cursor.getColumnIndex(
"phone"
));
24.
holder.item_tv_name.setText(name);
25.
holder.item_tv_phone.setText(phone);
26.
}
27.
28.
};
29.
30.
};
另一种是使用SimpleCursorAdapter,简化了代码,同样达到如上的效果
SimpleCursorAdapter继承了CursorAdapter,用法类似SimpleAdapter。
代码如下:
1.
public
void
createSimpleCursorAdapter(Cursor cursor)
2.
{
// SimpleCursorAdapter继承了CursorAdapter继承了BaseAdapter
3.
String[] from = {TABLE_NAME_NAME,TABLE_NAME_PHONE};
//列名与控件id一一对应
4.
int
[] to = {R.id.item_tv_name,R.id.item_tv_phone};
5.
//用的是SimpleCursorAdapter,用法和simpleAdapter相似
6.
mAdapter =
new
SimpleCursorAdapter(MainActivity.
this
, R.layout.listview_item, cursor, from, to);
7.
}
注意:使用SimpleCursorAdapter,时,创建表必须有以_id为列名的列
在使用时提示了一个错误:java.lang.IllegalArgumentException: column '_id' does not exist,
解决参考:http://blog.sina.com.cn/s/blog_6afeac500100ypzz.html
在使用SimpleCursorAdapter()显示SQLite数据库表中的数据的时候,Eclipse没有代码部分的错误提示,但程序会莫名其妙在运行时出错,而我们又找不到症结所在,报错如下:
显示:java.lang.IllegalArgumentException
而假如我们的数据表列名中并未定义名为“_id”的列名,如:我的数据表中定义了三列,列名分别为:“_id1”,”name1”,”bir”,这样,我的表根本和“_id”的列没有关系,那么上面的报错由何而来?
(ps:如果你的数据表中刚好就把某一列定义成了_id,那么肯定是不会出现这种错误了)
查找原因:
仔细查看Android的开发文档中有关SimpleCursorAdapter()的说明发现,SimpleCursorAdapter()的父类是CursorAdapter(),CursorAdapter()有一个很隐蔽的规定:

上 面显示:The Cursor must include a column named "_id" or this class will not work.也就是说,我们查询结果集游标Cursor返回的数据中,一定要有一列名为“_id”,否则这个类将不起作用,这也就是为什么Eclipse会 报上面的错误。
(ps:chi数据表三列名为:_id1,name1,bir)
原代码:
view plaincopy to clipboardprint?
1.
2.
3.
4.
5.
修改后代码:
view plaincopy to clipboardprint?
1.
2.
3.
4.
5.
在查询语句里改了以后,还要在相应的输出设置里面改,