如何从 FragmentActivity 更新 ListFragment 中的 ListView? [英] How update ListView in ListFragment from FragmentActivity?
问题描述
我在 FragmentActivity 中使用 ListFragment 以及 SimpleCursorAdapter 和修改后的 CursorLoader.修改后的 CursorLoader 只是发出 rawQueries - 没有其他变化.
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.
在 FragmentActivity 的某个时刻,我需要重新获取为 ListFragment 中的 ListView 提供数据的数据/游标.
At some point in the FragmentActivity I need to refetch the data/cursor that feeds the ListView in the ListFragment.
我该怎么做?
非常感谢.
这是调用 ListFragment 中的方法的 FragmentActivity:
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();
}
}
}
这里是 ListFragment,它的方法应该启动 ListView 的重新查询、重新加载或重新绘制.ListView.invalidate() 没有帮助 - 它没有改变显示的数据.
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);
}
}
推荐答案
尝试调用方法 restartLoader(MyConstants.LDR_TABLE1LIST, null, this);
刚刚提供的 LoaderManager在您的 requeryList()
方法中.
Try calling the method restartLoader(MyConstants.LDR_TABLE1LIST, null, this);
the LoaderManager just provided in your requeryList()
method.
这篇关于如何从 FragmentActivity 更新 ListFragment 中的 ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!