我有一个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);
}
这是我的Adapter
:
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();
}
}
}
如果我将 Adapter 切换为 simple ArrayAdapter
,它可以正常工作。
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, new String[]{"A", "B", "C"})
我绝望了,我不知道为什么会这样。