Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

android - How update ListView in ListFragment from FragmentActivity?

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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...