How update ListView in ListFragment from FragmentActivity?

a 夏天 提交于 2019-12-18 04:42:37

问题


I'm using a ListFragment within an FragmentActivity together with a SimpleCursorAdapter and a modified CursorLoader. The modified CursorLoader simply issues rawQueries - no other changes.

At some point in the FragmentActivity I need to refetch the data/cursor that feeds the ListView in the ListFragment.

How can I do that?

Many thanks in advance.

Here's the FragmentActivity calling a method in the ListFragment:

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();
        }
    }
}

And here's the ListFragment with the method that should initiate a re-query, re-load or re-paint of the ListView. ListView.invalidate() did not help - it did not change the shown data.

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);
    }
}

回答1:


Try calling the method restartLoader(MyConstants.LDR_TABLE1LIST, null, this); the LoaderManager just provided in your requeryList() method.




回答2:


You should not need to call notifyDataSetChanged(): the Loader takes care of that for you. Your code looks pretty good; I don't immediately see anything wrong. For reference, I suggest you compare your code to com.example.android.apis.app/LoaderThrottle.java in the API Demos sample app.




回答3:


Loaders will re-use a Cursor whenever possible. If you request a Loader for a set of data that has not changed, the loader is smart and return the Cursor via onLoadFinished without doing any additional work, not even setting the position of the Cursor to -1.

Try calling Cursor.moveToPosition(-1) in onLoadFinished() to work around this problem.



来源:https://stackoverflow.com/questions/10027202/how-update-listview-in-listfragment-from-fragmentactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!