10

我在 FragmentActivity 中使用 ListFragment 以及 SimpleCursorAdapter 和修改后的 CursorLoader。修改后的 CursorLoader 仅发出 rawQueries - 没有其他更改。

在 FragmentActivity 的某个时刻,我需要重新获取为 ListFragment 中的 ListView 提供数据/游标。

我怎样才能做到这一点?

提前谢谢了。

这是在 ListFragment 中调用方法的 FragmentActivity:

public class ActivityList extends FragmentActivity {

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
        ...
        processUpdateList();
    }

    ...

    private void processUpdateList() {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentlist);
        if (fragment != null) {
            ((FragmentList) fragment).requeryList();
        }
    }
}

这里的 ListFragment 具有应该启动重新查询、重新加载或重新绘制 ListView 的方法。ListView.invalidate() 没有帮助 - 它没有更改显示的数据。

public class FragmentList extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private SimpleCursorAdapter adapter;
    private Context             context;
    private ListView            listView;

    public void requeryList() {
    // listView.invalidate(); didn't re-query
        // TODO: How???
    }

    @Override
    public void onActivityCreated(final Bundle bundle) {
        super.onActivityCreated(bundle);

        context = getActivity().getApplicationContext();

        listView = getListView();

        getActivity().getSupportLoaderManager().initLoader(MyConstants.LDR_TABLE1LIST, null, this);

        adapter = new SimpleCursorAdapter(context,
                                          R.layout.fragmentlist_row,
                                          null,
                                          new String[] { Table1.DESCRIPTION },
                                          new int[] { R.id.fragmentlist_row_description },
                                          CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        setListAdapter(adapter);
        setListShown(false);

        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    @Override
    public Loader<Cursor> onCreateLoader(final int id, final Bundle bundle) {
        MyCursorLoader loader = null;

        switch (id) {
            case MyConstants.LDR_TABLE1LIST:
                loader = new MyCursorLoader(context,
                                            MySQLiteOpenHelper.TABLE1_FETCH,
                                            null);
                break;
        }

        return loader;
    }

    @Override
    public void onLoaderReset(final Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }

    @Override
    public void onLoadFinished(final Loader<Cursor> loader, final Cursor cursor) {
        adapter.swapCursor(cursor);

        setListShown(true);
    }
}
4

3 回答 3

1

尝试调用LoaderManager刚刚在您的方法中提供的restartLoader(MyConstants.LDR_TABLE1LIST, null, this);方法。requeryList()

于 2012-12-05T08:06:10.157 回答
0

您不需要调用notifyDataSetChanged():加载程序会为您处理。你的代码看起来不错;我没有立即看到任何问题。作为参考,我建议您将您的代码com.example.android.apis.app/LoaderThrottle.javaAPI Demos示例应用程序中的代码进行比较。

于 2012-08-08T00:00:54.327 回答
0

加载程序将尽可能重用光标。如果你为一组没有变化的数据请求加载器,加载器是聪明的,通过 onLoadFinished 返回游标而不做任何额外的工作,甚至不将游标的位置设置为 -1。

尝试致电Cursor.moveToPosition(-1)解决onLoadFinished()此问题。

于 2013-05-25T20:29:36.317 回答