我已經設置了一個ActionMode
回調,用作ActionBarSherlock
使用項目中的上下文ActionBar
(CAB)。爲什麼在ActionMode中isItemChecked()返回true?
我試圖設置多個選擇,以便我可以刪除ListView
中的多個項目。
我注意到在調試時,當環境ActionBar
(CAB)是不開放的,我叫isItemChecked()
一個列表項,我摸,它因爲它應該返回false。但是當CAB打開時,我接觸到的項目(我以前沒有碰過)在撥打isItemChecked()
時返回true。然後,當我調用getCheckedItemIds()
的數組上的delete時,該數組不包含以前返回爲isItemChecked()
的項。
有沒有人看到這個?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.habit);
habitListView = (ListView)findViewById(R.id.habitsListView);
habitListView.setAdapter(mAdapter);
habitListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
habitListView.setItemsCanFocus(false);
habitListView.setOnItemLongClickListener(new MyOnItemLongClickListener());
habitListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (habitListView.isItemChecked(position)) { // for debugging, returns false here
// when CAB isnt up.
int h = 2;
}
// handle differently when CAB is on.
if (mMode != null) {
if (habitListView.isItemChecked(position)) { // returns true here when CAB is up
// but this is the first time I'm
// clicking the item
habitListView.setItemChecked(position, false);
// turn CAB off if this is the last to be unchecked
if (habitListView.getCheckedItemIds().length == 0) {
mMode.finish();
}
} else {
habitListView.setItemChecked(position, true);
}
} else {
// start detail/edit view activity
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_create:
Habit test = new Habit("FLOSS", "GOOD", "", "");
mDbHelper.createHabitEntry(test);
mAdapter.changeCursor(mDbHelper.getAllEntries());
break;
}
return false;
}
private class MyOnItemLongClickListener implements OnItemLongClickListener {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
habitListView.setItemChecked(position, true);
mMode = startActionMode(new MyActionModeCallback());
return true;
}
}
private class MyActionModeCallback implements ActionMode.Callback {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
habitListView.setOnItemLongClickListener(new MyOnItemLongClickListener());
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.main_long_click_context, menu);
habitListView.setOnItemLongClickListener(null);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
long[] selected = habitListView.getCheckedItemIds();
if (selected.length > 0) {
for (long id : selected) {
mDbHelper.deleteEntry(id);
}
}
mAdapter.changeCursor(mDbHelper.getAllEntries());
mode.finish();
break;
}
return true;
}
};