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()两个方法的用途

你可能感兴趣的:(C++,c,C#)