本文介绍了为什么isItemChecked()在ActionMode时返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了用作上下文的 ActionMode 回调动作条(CAB)内的 ActionBarSherlock 使用的项目。

我试图设置多个选择,这样我可以在删除多个项的的ListView

我注意到在调试时,当环境动作条(CAB)是不开放的,我称之为 isItemChecked()一个列表项,我摸,它返回false,因为它应该。但是,当CAB是开放的,我摸(我以前没有触及)项目上调用 isItemChecked返回true()。然后,当我打电话阵列上从 getCheckedItemIds删除(),数组不包含为$ P $为 pviously返回true isItemChecked项目( )

有没有人见过这个?

  @覆盖
保护无效的onCreate(捆绑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(假);
    habitListView.setOnItemLongClickListener(新MyOnItemLongClickListener());
    habitListView.setOnItemClickListener(新OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
            如果(habitListView.isItemChecked(位置)){//进行调试,在这里返回false
                                                         //当CAB心不是了。
                INT H = 2;
            }
            //不同的处理时,CAB上。
            如果(mMode服务!= NULL){
                如果(habitListView.isItemChecked(位置)){//当CAB高达返回true这里
                                                             //但这是我第一次
                                                             //单击项目
                    habitListView.setItemChecked(位置,FALSE);
                    //关闭CAB关闭,如果这是最后取消选中
                    如果(habitListView.getCheckedItemIds()==长度0){
                        mMode.finish();
                    }
                }其他{
                    habitListView.setItemChecked(位置,真正的);
                }
            }其他{
                //启动细节/编辑视图活动
            }
        }
    });
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getSupportMenuInflater()膨胀(R.menu.activity_main,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理项目选择
    开关(item.getItemId()){
    案例R.id.menu_create:
        习惯试验=新的习惯(FLOSS,好,,);
        mDbHelper.createHabitEntry(试验);
        mAdapter.changeCursor(mDbHelper.getAllEntries());
        打破;
    }
    返回false;
}私有类MyOnItemLongClickListener实现OnItemLongClickListener {
    @覆盖
    公共布尔onItemLongClick(适配器视图<>母公司,观景,INT位置,长的id){
        habitListView.setItemChecked(位置,真正的);
        mMode服务= startActionMode(新MyActionModeCallback());
        返回true;
    }
}私有类MyActionModeCallback实现ActionMode.Callback {
    @覆盖
    在prepareActionMode公共布尔(ActionMode模式,菜单菜单){
        // TODO自动生成方法存根
        返回false;
    }    @覆盖
    公共无效onDestroyActionMode(ActionMode模式){
        habitListView.setOnItemLongClickListener(新MyOnItemLongClickListener());
    }    @覆盖
    公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
        。mode.getMenuInflater()膨胀(R.menu.main_long_click_context,菜单);
        habitListView.setOnItemLongClickListener(NULL);
        返回true;
    }    @覆盖
    公共布尔onActionItemClicked(ActionMode模式,菜单项项){
        开关(item.getItemId()){
        案例R.id.menu_delete:
            长[] =选择habitListView.getCheckedItemIds();
            如果(selected.length大于0){
                为(长ID:选择){
                    mDbHelper.deleteEntry(ID);
                }
            }
            mAdapter.changeCursor(mDbHelper.getAllEntries());
            mode.finish();
            打破;
        }
        返回true;
    }
};


解决方案

那么是什么工作对我来说是只用

  habitListView.setOnItemClickListener(新OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,
                INT位置,长的id){            //不同的处理时,CAB上。
            如果(mMode服务!= NULL){                view.setSelected(view.isSelected()!);                updateCABtitle();                //关闭CAB关闭,如果这是最后取消选中
                    如果(habitListView.getCheckedItemIds()==长度0){
                        mMode.finish();
                    }            }其他{
                //启动细节/编辑视图活动
                意向意图=新意图(getApplicationContext(),HabitDetailActivity.class);
                startActivity(意向);            }        }    });

 私有类MyOnItemLongClickListener实现OnItemLongClickListener {    @覆盖
    公共布尔onItemLongClick(适配器视图<>母公司,观景,
                    INT位置,长的id){
            habitListView.setItemChecked(位置,真正的);            mMode服务= startActionMode(新MyActionModeCallback());
            updateCABtitle();            返回true;
    }}

我真的不明白,为什么 setItemChecked()不工作 onItemClick()但似乎是为 onItemLongClick()。它看起来好像有被调用的 AbsListView.PerformItemClick一个默认的点击处理程序()必须做的选中/取消什么的一些切换。

I've set up an ActionMode callback for use as the contextual ActionBar (CAB) within an ActionBarSherlock using project.

I'm trying to set up multiple select so that I can delete multiple items in a ListView.

I noticed while debugging, when the contextual ActionBar (CAB) is not open and I call isItemChecked() on a list item that I touch, it returns false as it should. But when the CAB IS open, items that I touch (that I haven't touched before) return true on calls to isItemChecked(). Then when I call delete on the array from getCheckedItemIds(), that array does not contain the items that were previously returning true for isItemChecked().

Has anyone seen this?

@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;
    }
};
解决方案

So what has worked for me is to just use

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

            }

        }

    });

and

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

}

I don't really understand why setItemChecked() is not working for onItemClick() but seems to be for onItemLongClick(). It looks as though there is a default click handler that gets called in AbsListView.PerformItemClick() that must do some toggling of checked/unchecked or something.

这篇关于为什么isItemChecked()在ActionMode时返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 00:56