0

这是我的 ListFragment 中的代码(我使用的是上下文菜单):

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
            .getMenuInfo();
    Log.d(TAG, "onContextItemSelected clicked");
    long id = info.id;
    Log.d(TAG, "list id " + id);

    int menuItemIndex = item.getItemId();
    Log.d(TAG, "menuItemIndex " + menuItemIndex);
    if (menuItemIndex == 0) {
        try {
            plantManager.deletePlantRelation(id);
            CharSequence text = getActivity().getString(
                    R.string.info_could_delete_plant);
            Toast toast = Toast.makeText(getActivity(), text,
                    Toast.LENGTH_LONG);
            toast.show();
            loader = getLoaderManager().restartLoader(0, null, this);
            myAdapter.notifyDataSetChanged();
        } catch (Exception ex) {
            ex.printStackTrace();
            CharSequence text = getActivity().getString(
                    R.string.info_could_not_delete_plant);
            Toast toast = Toast.makeText(getActivity(), text,
                    Toast.LENGTH_LONG);
            toast.show();
        }
    }
    return true;
}

该方法被执行,我得到了 Toast-Message - 但列表不会刷新。

期待您的帮助。

提前致谢!

更新:

public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {

        return new CursorLoader(getActivity()) {
            @Override
            public Cursor loadInBackground() {
                myCursor = plantManager.fetchPlantRelations();
                return myCursor;
            }
        };
    }
4

1 回答 1

0

您不仅不需要对 LoaderManager 进行重置,也不需要在适配器上进行 notifyDataSetChanged,您实际上也不应该这样做。执行此操作的正确方法是确保 fetchPlantRelations() 返回的游标已注册用于特定 URI 上的通知。如果您随后使对基础数据集的更改(例如由 deletePlantRelation 引起的更改)通知该 URI,则其余的将完全自动发生。在此处查看代码:

https://github.com/marakana/yamba/blob/yambaII/YambaService/src/com/marakana/android/yamba/svc/data/YambaProvider.java

尤其是第 182 行和第 242 行。这就是它的全部内容。

于 2013-02-28T21:14:10.510 回答