作为标题状态我将数据从数据库链接到列表视图时遇到了一些问题,问题是适配器将只设置返回的最后一行,而不是每个来自数据库表的行。Android:setListAdapter仅设置数据库中的最后一行
什么,我想实现的一个例子:
表的动物:猴子 猫 狗 虎
将显示为只老虎在屏幕上。
我的方法从数据库返回游标:
public Cursor retrieveAnimals(){
return = DB.query(ANIMAL_TABLE, new String[] {
KEY_ROWID,
KEY_ANIMALNAME,
},
null,
null,
null,
null,
null);
}
,并设置列表视图
dbm = new MyDBManager(this);
dbm.open();
dbm.deleteAnimals();
dbm.populateDB();
// after populating, set the adapter..
myCursor = dbm.getAllAnimals();
String[] columns = new String[] {"animal_name"};
int[] to = new int[] {R.id.animal};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, myCursor, columns, to);
this.setListAdapter(mAdapter);
我有一种感觉,我的问题出在光标位置,因为它会自动移动到最后一行设置适配器时,我一直在试图找到解决我的问题,但没有运气。
在此先感谢您的任何建议。
编辑:方法来填充DB
public long populateDB(){
ContentValues initialValues = new ContentValues();
for(int i = 0; i < animalName.length; i++){
initialValues.put(KEY_ANIMALNAME, animalName[i]);
}
return DB.insert(ANIMAL_TABLE, null, initialValues);
}
'myCursor.getCount()'返回什么? – rajath 2011-04-05 13:48:05