从FragmentActivity在ListFragment如何更新的ListView?FragmentActivity、ListFragment、ListView

2023-09-04 23:54:20 作者:姐的美无可挑剔

我使用的是ListFragment的FragmentActivity内连同SimpleCursorAdapter和修改CursorLoader。修改后的CursorLoader根本问题rawQueries - 没有其他变化

在我需要重新获取数据的饲料ListView控件在ListFragment的FragmentActivity /光标某一点。

我怎么能这样做?

在预先感谢。

这里的FragmentActivity要求在ListFragment的方法:

 公共类ActivityList扩展FragmentActivity {

    @覆盖
    保护无效onActivityResult(最终诠释请求code,最终诠释结果code,最终意向意图){
        ...
        processUpdateList();
    }

    ...

    私人无效processUpdateList(){
        片段片段= getSupportFragmentManager()findFragmentById(R.id.fragmentlist)。
        如果(片段!= NULL){
            ((FragmentList)片段).requeryList();
        }
    }
}
 

下面是该方法的ListFragment应该发起一个重新查询,重新加载或重新油漆的ListView。 ListView.invalidate()并没有帮助 - 它并没有改变显示的数据

 公共类FragmentList扩展ListFragment实现LoaderManager.LoaderCallbacks<光标> {

    私人SimpleCursorAdapter适配器;
    私人上下文的背景下;
    私人的ListView ListView的;

    公共无效requeryList(){
    // listView.invalidate();没有重新查询
        // TODO:如何???
    }

    @覆盖
    公共无效onActivityCreated(最终束束){
        super.onActivityCreated(包);

        上下文= getActivity()getApplicationContext()。

        ListView控件= getListView();

        。getActivity()getSupportLoaderManager()initLoader(MyConstants.LDR_TABLE1LIST,空,这一点)。

        适配器=新SimpleCursorAdapter(背景下,
                                          R.layout.fragmentlist_row,
                                          空值,
                                          新的String [] {} Table1.DESCRIPTION,
                                          新的INT [] {} R.id.fragmentlist_row_description,
                                          CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        setListAdapter(适配器);
        setListShown(假);

        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    @覆盖
    公共装载机<光标> onCreateLoader(最终诠释ID,最后的捆绑包){
        MyCursorLoader装载机= NULL;

        开关(ID){
            案例MyConstants.LDR_TABLE1LIST:
                装载机=新MyCursorLoader(背景下,
                                            MySQLiteOpenHelper.TABLE1_FETCH,
                                            空值);
                打破;
        }

        返回装载机;
    }

    @覆盖
    公共无效onLoaderReset(最终装载机<光标>装载机){
        adapter.swapCursor(空);
    }

    @覆盖
    公共无效onLoadFinished(最终装载机<光标>装载机,最后光标光标){
        adapter.swapCursor(光标);

        setListShown(真正的);
    }
}
 
UI碎片控件之Fragment基本概述

解决方案

尝试调用方法 restartLoader(MyConstants.LDR_TABLE1LIST,空,这一点); 在 LoaderManager 只是提供你的 requeryList()方法。

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

解决方案

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