我正在使用FragmentActivity中的ListFragment与SimpleCursorAdapter和修改的CursorLoader.修改的CursorLoader只是发出rawQueries – 没有其他更改.
在FragmentActivity中的某个时刻,我需要重新获取ListFragment中的ListView的数据/游标.
我怎样才能做到这一点?
提前谢谢了.
这是FragmentActivity调用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(); } } }
这里是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,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); } }