gpt4 book ai didi

android - 为什么 isItemChecked() 在 ActionMode 中返回 true?

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:36 24 4
gpt4 key购买 nike

我已经设置了一个 ActionMode 回调,用作 ActionBarSherlock using 项目中的上下文 ActionBar (CAB)。

我正在尝试设置多选,以便我可以删除 ListView 中的多个项目。

我在调试时注意到,当上下文 ActionBar (CAB) 未打开并且我在我触摸的列表项上调用 isItemChecked() 时,它返回 false 作为这应该。但是当 CAB 打开时,我触摸的项目(我以前没有触摸过的)在调用 isItemChecked() 时返回 true。然后,当我从 getCheckedItemIds() 对数组调用 delete 时,该数组不包含之前为 isItemChecked() 返回 true 的项目。

有没有人看过这个?

@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;
}
};

最佳答案

所以对我有用的就是使用

   habitListView.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

//handle differently when CAB is on.
if (mMode != null){

view.setSelected(!view.isSelected());

updateCABtitle();

//turn CAB off if this is the last to be unchecked
if (habitListView.getCheckedItemIds().length == 0){
mMode.finish();
}

} else {
//start detail/edit view activity
Intent intent = new Intent(getApplicationContext(), HabitDetailActivity.class);
startActivity(intent);

}

}

});

    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());
updateCABtitle();

return true;
}

}

我不太明白为什么 setItemChecked()onItemClick() 不起作用,但似乎对 onItemLongClick() 起作用。看起来好像有一个在 AbsListView.PerformItemClick() 中调用的默认点击处理程序,它必须执行一些选中/未选中或其他操作的切换。

关于android - 为什么 isItemChecked() 在 ActionMode 中返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14328869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com