Cursor上用法的注意事项

注意要判断Cursor返回的记录是否为空~~ 之前给这个问题卡了很久~ 囧
public SimpleAdapter getlistItem(Cursor c){

ArrayList<HashMap<String,Object>> listItem = new ArrayList<HashMap<String,Object>>();
if (c.getCount()==0) //一定要加这个判断条件~,否则出现cursor越界
return null;
c.moveToFirst();
do
{
int index;
HashMap<String, Object> map = new HashMap<String, Object>();
index = c.getColumnIndex(Todolist.TITLE);
map.put(Todolist.TITLE, c.getString(index));
index = c.getColumnIndex(Todolist.BEGIN_TIME);
map.put(Todolist.BEGIN_TIME,convertTime(c.getLong(index)));
index = c.getColumnIndex(Todolist.STATUS);
map.put(Todolist.STATUS, drawble[c.getInt(index)]);
listItem.add(map);
}
while (c.moveToNext());

adapter = new SimpleAdapter(this, listItem, R.layout.list_item,
new String[]{Todolist.TITLE,Todolist.BEGIN_TIME,Todolist.STATUS},
new int[]{R.id.TextView01,R.id.TextView02,R.id.ImageView01});
return adapter;
}

还有就是查询设定某些条件返回的cursor,不能直接就获取返回记录的的值,要先moveToFirst()
例如:
PersonActivity.gategory = PersonActivity.dc.query(CategoryList.projection,"_id="+tag, null, null);
/*
* 记得移动光标,因为刚开得到的cursor没有指向任何记录位置的~ 惨痛的教训!
*/
PersonActivity.gategory.moveToFirst();
showtype.setText("类型:"+PersonActivity.gategory.getString(1));


还有Cursor的requery()和deactivate()两个方法的用途
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值