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);
我感觉我的问题出在光标位置,因为它在设置适配器时自动移动到最后一行,我一直在尝试找到问题的解决方案但没有运气。
预先感谢您的任何建议。
编辑:填充数据库的方法
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);
}
as the title states I am having a little trouble linking my data from a database to a listview, the problem being that the adapter will set only the last row returned, as opposed to each row from the database table.
An example of what I am trying to achieve:
TABLE ANIMAL:
Monkey
Cat
Dog
Tiger
will show up as only Tiger on screen.
My method returning cursor from the database:
public Cursor retrieveAnimals(){
return = DB.query(ANIMAL_TABLE, new String[] {
KEY_ROWID,
KEY_ANIMALNAME,
},
null,
null,
null,
null,
null);
}
and setting the listview
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);
I have a feeling my problem lies in the cursor position, in that it is moving automatically to the last row when setting the adapter, I have been trying to find a solution to my problem but with no luck.
Thanks in advance for any suggestions.
EDIT: Method to populate 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的
retrieveAnimals()
中,该行应该是return DB.query(...)
(请注意=
之间缺少=
>返回和DB.query
)。除此之外,你的代码看起来不错。您应该在光标上调用
startManagingCursor
(在调用getAllAnimals
之后)以避免内存泄漏。编辑:将您的填充方法更改为:
或者(更多内存使用,但如果您需要在插入某些记录之前修改它们,则可能有用):
In your
retrieveAnimals()
, the line should bereturn DB.query(...)
(note the lack of an=
betweenreturn
andDB.query
).Apart from that your code looks fine. You should call
startManagingCursor
on the cursor (after callinggetAllAnimals
) to avoid memory leaks.Edit: change your populate method to:
Alternatively (more memory usage, but useful perhaps if you need to modify some of the records before inserting them):
你尝试过吗
Did you try