CursorAdapter的突破CHOICE_MODE_MULTIPLE选项 [英] CursorAdapter breaks CHOICE_MODE_MULTIPLE option
问题描述
我有一个 ListFragment
,其中我添加了一个的CursorAdapter
我的的ListView
,我希望能够点击几行,使用上下文操作栏。我用SherlockActionbar,它工作正常,当我用一个简单的 ArrayAdapter
。但是,当我切换到的CursorAdapter
,它打破。我不能选择多行,只有一个。任何想法,为什么它会发生?
I have a ListFragment
, where I add a CursorAdapter
to my ListView
, and I want to be able to click several rows, to use contextual action bar. I use SherlockActionbar, and it works fine, when I use a simple ArrayAdapter
. But when I switch to CursorAdapter
, it breaks. I cannot select multiple rows, only one. Any idea why it might happen?
在 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:
And finally the 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
,它工作正常。
If I switch my Adapter to a simple ArrayAdapter
, it works fine.
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, new String[]{"A", "B", "C"})
我绝望了,我不知道,为什么发生这种情况。
I am hopeless, and I have no idea, why this is happening.
推荐答案
为了对 ListView.CHOICE_MODE_MULTIPLE
模式下正常工作,每个项目中的适配器必须返回从 getItemId()
方法的独特的价值。
In order for the ListView.CHOICE_MODE_MULTIPLE
mode to work properly, each of the item in the adapter must return a unique value from the getItemId()
method.
您正在使用您的适配器光标在这些线路上产生的:
The cursor you are using for your adapter is produced on these lines:
FinTracDatabase database = mDatabaseProvider.get();
Cursor cursor = database.getTransactionCursor(false);
你能检查是否有对各行其_id列唯一值?我怀疑,他们都有着相同的价值观,导致您看到的行为。
Could you check whether it has unique values on its "_id" column for each row? I am suspecting that they all share the same values, resulting in the behavior that you see.
这篇关于CursorAdapter的突破CHOICE_MODE_MULTIPLE选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!