设置Android ListActivity起始位置
我在 ListActivity 中显示来自 SQLite 的数据。用户可以单击列表元素,将其发送到更新数据库的活动。
更新 Activity 完成后,用户将返回到原始 ListActivity。
当用户从 ListActivity 返回时,我希望将 ListView 定位为显示她刚刚更新的数据(例如,在可能的第一个位置)。
我尝试将光标定位到我想要显示的条目;但这似乎不起作用(请参阅下面的代码以获取代码示例)。
protected void onResume() {
super.onResume();
mDbCursor = mDbAdapter.getCursor();
long id = getIntent().getLongExtra(FIRST_ID, -1); // Id of updated row.
while (mDbCursor.moveToNext() && getId(mDbCursor) != id) {
}
.
. calling setListAdapter(SimpleCursorAdapter);
.
}
我确实认为退出 while 循环后光标定位到正确的行。
I'm displaying data coming from SQLite in a ListActivity. The user can click on an list element which sends him to an Activity that updates the DB.
Once the update Activity is finished, the user gets sent back to the original ListActivity.
When the user comes back from the ListActivity I would like the ListView to be positioned to display the data that she just updated (e.g. in the first position of possible).
I tried positioning the Cursor to point to the entry I want displayed; but that doesn't seem to work (see code below for code example).
protected void onResume() {
super.onResume();
mDbCursor = mDbAdapter.getCursor();
long id = getIntent().getLongExtra(FIRST_ID, -1); // Id of updated row.
while (mDbCursor.moveToNext() && getId(mDbCursor) != id) {
}
.
. calling setListAdapter(SimpleCursorAdapter);
.
}
I do think the cursor is positioned to the correct row after exiting the while loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过在 ListActivity 上使用
getListView().setSelection(intposition)
?列表的position
参数从 0 开始,因此它与您的id
变量不同,但在 while 循环中,您可以在迭代时从零开始计数光标并将其用作位置。Have you tried using
getListView().setSelection(int position)
on your ListActivity? Theposition
argument starts at 0 for the list, so it's not the same thing as yourid
variable, but in your while loop you could count from zero as you iterate through the Cursor and use that as the position.不考虑光标初始位置。你必须使用listView.setSelection(intposition),并根据你的id获取位置:
The cursor initial position is not considered. You have to use listView.setSelection(int position), and get the position according to your id: