具有多个ListFragments的Android SherlockFragmentActivity

时间:2012-06-22 16:57:51

标签: android android-fragments actionbarsherlock simplecursoradapter android-loadermanager

我有一个显示几个标签的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;
    }

}

任何帮助都非常感谢..

1 个答案:

答案 0 :(得分:1)

This question is essentially answered here

答案中概述了您需要了解的所有内容。你应该做几个修复:


  • 您应该首先传递CursorAdapter一个null光标。 LoaderManagerCursorLoader为您执行初始查询。 (见上面的答案)。另请注意,您当前使用的构造函数是deprecated。您应该使用this one代替(将0作为标志传递)。

    cursorAdapter = new SimpleCursorAdapter(         getActivity()         R.layout.list_item,         空值,         从,         至,         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