android - CursorAdapter 破坏了 CHOICE_MODE_MULTIPLE 选项

标签 android listview android-arrayadapter android-cursoradapter

我有一个 ListFragment,我在其中添加一个 CursorAdapter 到我的 ListView,并且我希望能够单击几行,以使用上下文操作栏。我使用 SherlockActionbar,当我使用简单的 ArrayAdapter 时,它工作得很好。但是当我切换到 CursorAdapter 时,它就崩溃了。我无法选择多行,只能选择一行。知道为什么会发生这种情况吗?

onActivityCreated中我设置了列表:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActionMode = null;
    mListView = getListView();
    FinTracDatabase database = mDatabaseProvider.get();
    Cursor cursor = database.getTransactionCursor(false);
    mCursorAdapter = new TransactionListAdapter(getSherlockActivity(), cursor);
    mListView.setAdapter(mCursorAdapter);
    mListView.setItemsCanFocus(false);
    mListView.setOnItemClickListener(this);
    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

这是我的适配器:

private class TransactionListAdapter extends CursorAdapter {

    public TransactionListAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        bindToExistingView(view, cursor);
    }

    private void bindToExistingView(View view, Cursor cursor) {
        CheckedTextView amountView = (CheckedTextView) view;
        amountView.setText(cursor.getString(cursor.getColumnIndex(Transactions.TITLE)));
    }

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
        LayoutInflater layoutInflater = getSherlockActivity().getLayoutInflater();
        View view = layoutInflater.inflate(android.R.layout.simple_list_item_multiple_choice, arg2, false);
        bindToExistingView(view, arg1);
        return view;
    }

}

最后是 onClickListener:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    SparseBooleanArray checked = mListView.getCheckedItemPositions();
    boolean hasCheckedElement = true;
    for (int i = 0; i < checked.size() && !hasCheckedElement; i++) {
        hasCheckedElement = checked.valueAt(i);
    }

    if (hasCheckedElement) {
        if (mActionMode == null) {
            mActionMode = getSherlockActivity().startActionMode(new SelectingActionMode());
        }
    } else {
        if (mActionMode != null) {
            mActionMode.finish();
        }
    }
}

如果我将适配器切换为简单的ArrayAdapter,它就可以正常工作。

new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, new String[]{"A", "B", "C"})

我很绝望,我不知道为什么会发生这种情况。

最佳答案

为了 ListView.CHOICE_MODE_MULTIPLE要正常工作,适配器中的每个项目都必须从getItemId()返回一个唯一的值。方法。

您用于适配器的光标是在这些行上生成的:

  FinTracDatabase database = mDatabaseProvider.get();
  Cursor cursor = database.getTransactionCursor(false);

您能否检查每行的“_id”列是否具有唯一值?我怀疑他们都有相同的值(value)观,从而导致了您所看到的行为。

关于android - CursorAdapter 破坏了 CHOICE_MODE_MULTIPLE 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11953888/

相关文章:

android - 我应该在 IntentService 中使用 WakeLock 吗?

android - 从 android 中的未决 Intent 更新警报

android - FlurryAgent.onPageView() 是如何工作的?

android - 拖动排序 ListView 项目单击不起作用

c - 我无法阻止用户在 ListView 中编辑标签

java - 向适配器添加新项目时 ArrayAdapter 崩溃

java - ArrayList 中每个对象的删除按钮

java - Android 与多种设备的兼容性

Android 使用游标适配器在 ListView 中保存复选框状态

android - 在不覆盖 getFilter 方法的情况下使用 ArrayAdapter 过滤 ListView