在其适配器上使用changeCursor()后ListView不会刷新
我正在使用数据库,并且有一个列表片段,它使用游标适配器通过在加载器中查询数据库来获取。 当用户长按列表视图项时,他会看到上下文菜单和删除该条目的选项。 当他按下删除键时,我将启动一个线程来删除该条目,然后再次启动加载程序以获取新的光标(因为“requery()”已被弃用)。 当加载程序完成加载新光标时,我尝试使用 changeCursor
方法来刷新列表视图,但它没有这样做,所以我尝试使用 notifyDataSetChanged
> 但它也不起作用。 我已经检查过,光标从加载器返回,它确实改变了光标,但列表视图没有刷新。
我应该怎么办?重新启动片段?
这是一些代码:
case MENU_REMOVE:
final AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
mProgress = ProgressDialog.show(getActivity(), getString(R.string.list_remove_progress_title_text),
getString(R.string.progress_dialog_description));
new Thread(new Runnable() {
@Override
public void run() {
PlacesHandler wph = new PlacesHandler(getActivity());
wph.DeleteEntry(mPlaceName, info.id);
getLoaderManager().initLoader(0, null, ListFragment.this);
}
}).start();
return(true);
加载完成后:
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if(mProgress == null || !mProgress.isShowing()){
...
}
else{
mProgress.dismiss();
mAdapter.changeCursor(data);
mAdapter.notifyDataSetChanged();
}
}
谢谢!
I'm using a database, and there's a list fragment which use a cursor adapter to I get by querying the databse in a loader.
When the user press a list view item long press, he sees a context menu and a option to delete this entry.
When he press delete I'm starting a thread that deletes this entry and then start the loader again to get a new cursor (becuase "requery()" is deprecated).
When the loader finishes to load the new cursor I'm trying to use changeCursor
method that suppose to refresh the list view but it doesn't do it, so I tried using notifyDataSetChanged
but it doesn't work too.
I've checked and the cursor coming back from the loader and it does change the cursor but the list view doesn't refresh.
What should I do? Restart the fragment?
Here's some code:
case MENU_REMOVE:
final AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
mProgress = ProgressDialog.show(getActivity(), getString(R.string.list_remove_progress_title_text),
getString(R.string.progress_dialog_description));
new Thread(new Runnable() {
@Override
public void run() {
PlacesHandler wph = new PlacesHandler(getActivity());
wph.DeleteEntry(mPlaceName, info.id);
getLoaderManager().initLoader(0, null, ListFragment.this);
}
}).start();
return(true);
When finished loading:
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if(mProgress == null || !mProgress.isShowing()){
...
}
else{
mProgress.dismiss();
mAdapter.changeCursor(data);
mAdapter.notifyDataSetChanged();
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通过创建新的 curson 和新的适配器解决了类似的问题。然后将新适配器设置到列表视图:
myListView.setAdapter(listViewAdapter);
这种方法的唯一问题是我的列表视图不保持滚动位置。
I solved the similar problem by creating new curson and new adapter. And then setting new adapter to the list view:
myListView.setAdapter(listViewAdapter);
The ony problem of this approach is that my list view doesn't keep scroll position.
谢谢大家,但我发现了我的问题!
从加载器返回的光标与适配器中的光标相同。
这是因为我调用的是:
而不是:
所以因为当我在创建片段时第一次加载游标时我已经使用了ID为0的加载器,所以它立即返回了我游标。
因此,如果有人遇到同样的问题并阅读它,只需知道
initLoader
使用具有指定 ID 的现有加载程序(如果有)。如果没有,它会创建一个。但如果您想丢弃加载程序的旧数据并重新开始,您应该使用
restartLoader
。再次感谢任何试图提供帮助的人!
Thanks everyone but I found my problem!
The cursor that was returned from the loader was the same cursor as what was in the adapter.
This is because I called:
instead of:
So because I've already used the loader with the ID 0 when I first loaded the cursor when the fragment was created, it just returned me the cursor immediately.
So if some people face the same problem and read it, just know that
initLoader
uses an existing loader with the specified ID if there is one. If there isn't, it creates one.But if you want to discard the loader's old data and start over you should use
restartLoader
.Again, thanks for anyone who tried to help!
删除 SQLite 寄存器后,我简单地执行了以下操作:
After deleting a SQLite register, I simply did this:
有几件事。我对 Fragment 不熟悉,所以我会尽力帮助你。
您在哪里更新游标以获取新数据?另外,尝试使用 AsyncTask 来执行删除。完成后,在 postexecute 方法中更新适配器。
Couple of things. I'm not familiar with fragments, so I'll just try to help you out.
Where are you updating the cursor to fetch the new data? Also, try using an AsyncTask to do the deleting. When done, update the adapter in the postexecute method.