【发布时间】:2011-04-06 17:59:12
【问题描述】:
正如标题所述,我在将数据从数据库链接到列表视图时遇到了一点问题,问题在于适配器将只设置返回的最后一行,而不是数据库表中的每一行。
我正在努力实现的一个例子:
餐桌动物: 猴 猫 狗 老虎
将在屏幕上仅显示为 Tiger。
我的方法从数据库返回游标:
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);
我感觉我的问题在于光标位置,因为在设置适配器时它会自动移动到最后一行,我一直在尝试找到解决问题的方法,但没有成功。
提前感谢您的任何建议。
编辑:填充数据库的方法
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()返回什么?
标签: android database listview adapter