![](/img/trans.png)
[英]SherlockFragmentActivity with multiple ListFragments and SQLite cursor battles
[英]Android SherlockFragmentActivity with multiple ListFragments
我有一个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;
}
}
任何帮助,不胜感激..
基本上,答案中概述了您需要了解的所有内容。 您应该进行几个修复:
您应该首先向CursorAdapter
传递一个null
游标。 LoaderManager
将具有CursorLoader
来为您执行初始查询。 (请参阅上面的答案)。 另请注意, 不建议使用当前使用的构造函数。 您应该改用这个 (将0
作为标志传递)。
cursorAdapter = new SimpleCursorAdapter(getActivity(),R.layout.list_item,null,from,to,0);
删除此行:
getActivity().startManagingCursor(venuesCursor);
LoaderManager
的全部要点是它为您管理光标。 您不需要“将光标移到系统上进行管理” ...这正是 LoaderManager
的工作。 :)
由于我在#1和#2中描述的原因,看起来这一行代码是不必要的:
venuesCursor = getDatasource().getAllVenues( ((EventActivity) getActivity()).getEventId());
onItemClick
。 由于您使用的是ListFragment
,因此我怀疑您想替代onListItemClick
。 我不确定为什么要包括这些行,但是看起来您也想删除它们。
Cursor cursor = cursorAdapter.getCursor(); cursor.moveToPosition(position);
在大多数情况下,您不应该操作适配器的游标,因为它是由内部系统管理的并且绑定到LoaderManager
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.