我有一个显示几个选项卡的 SherlockFragmentActivity。每个选项卡都是 ListFragment。
每个 ListFragment 都是这样创建的:
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.setDisplayHomeAsUpEnabled(true);
bar.setDisplayShowTitleEnabled(true);
// users event list
bar.addTab(bar.newTab()
.setTag("contacts_list")
.setText(getString(R.string.list_contacts_header))
.setTabListener(new TabListener<ContactListFragment>(
this, getString(R.string.list_events_header), ContactListFragment.class, null)));
然后,每个 ListFragments 都像这样加载:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// database cursor containing all venues for this event
venuesCursor = getDatasource().getAllVenues(((EventActivity) getActivity()).getEventId());
// hand the cursor to the system to manage
getActivity().startManagingCursor(venuesCursor);
// bind the columns of the cursor to the list
String[] from = new String[] { VenuesDataSource.KEY_NAME, VenuesDataSource.KEY_DESCRIPTION };
int[] to = new int[] { R.id.list_item_title, R.id.list_item_subtitle };
cursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item, venuesCursor, from, to);
// retrieve the listview to populate
ListView lv = (ListView) getActivity().findViewById(android.R.id.list);
// set the adapter on the listview
lv.setAdapter(cursorAdapter);
// click event for each row of the list
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
Cursor cursor = cursorAdapter.getCursor();
cursor.moveToPosition(position);
Toast.makeText(getActivity(), "Tapped row " + position + "!", Toast.LENGTH_SHORT).show();
}
});
// Start out with a progress indicator.
setListShown(false);
// prepare the loader -- either re-connect with an existing one, or start a new one.
// getLoaderManager().initLoader(0, null, this);
// load the data
getActivity().getSupportLoaderManager().initLoader(0, null, this);
}
就像我说的,这个活动有多个以 ListFragments 形式存在的选项卡。我遇到的问题是,当单击选项卡以选择它们时,我得到
E/AndroidRuntime(2519): java.lang.IllegalArgumentException: column 'name' does not exist
这是错误的,我用 adb 来查看数据库,它抱怨的列是 100% 在那里,所以它必须与不关闭游标或其他东西有关,并且当上面加载它时实际使用错误光标。
编辑:添加 CursorLoader 代码
public static final class VenueCursorLoader extends SimpleCursorLoader {
Context mContext;
public VenueCursorLoader(Context context) {
super(context);
mContext = context;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = null;
VenuesDataSource datasource = new VenuesDataSource(mContext);
// TODO: provide the event_id to the getAllVenues method
cursor = datasource.getAllVenues(((EventActivity) mContext).getEventId());
if (cursor != null) {
cursor.getCount();
}
return cursor;
}
}
非常感谢任何帮助..