【问题标题】:Android CheckedTextViewAndroid CheckedTextView
【发布时间】:2012-09-08 08:01:03
【问题描述】:

我有一个包含checkedtextviewlistview。我的应用程序从列表视图的顶部移动到底部。我想在调用操作之前检查是否检查了该项目。如果未选中,我想移至列表中的下一项。

例如

第 1 项 - 已检查

第 2 项 - 已选中

第 3 项 - 未选中

第 4 项 - 已检查

所以,我希望应用程序如下处理:

项目 1

第 2 项

第 4 项。

我不确定如何从listview 位置访问项目的已检查状态。

我想要的逻辑如下:

Is Current Item checked?
Yes:
Call action
No:
Move to next item.
Reloop to top of void.

我需要一些东西来阻止无限循环。

【问题讨论】:

    标签: android listview android-listview checkedtextview


    【解决方案1】:

    1.)首先创建一个数组,表示适配器中的项目检查状态

    (假设您为此目的扩展了 BaseAdapter 类):

    private boolean [] itemsChecked = new boolean [getCount()];
    

    2.) 然后创建一个OnCheckedChangeListener

    private OnCheckedChangeListener listener = new OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton button, boolean checked)
        {
            Integer index = (Integer)button.getTag();
            itemsChecked[index] = checked;
        }
    }
    

    3.) 在您的适配器getView() 方法中:

    public View getView(int index, View view, ViewGroup parent)
    {
        /*...*/
        CheckBox checkBox = /*get the checkbox*/;
        checkbox.setTag(index); 
        checkBox.setOnCheckedChangeListener(listener);
        /*...*/
    }
    

    4.)onClick() 方法中:

    public void onClick(View view)
    {
        //just get the boolean array somehow
        boolean [] itemsChecked = adapter.getItemsCheckedArray(); 
    
        for(int i=0; i<itemsChecked.length; i++)
        {
            if(itemsChecked[i])
            {
                //the i th item was checked
            }
            else
            {
                    //it isnt checked
            } 
        }
    }
    

    【讨论】:

      【解决方案2】:

      一种解决方案是使用ArrayList 的职位。 当用户选中/取消选中checkbox 时,相应地添加/删除ArrayList 中的位置。 然后当用户结束时,只需遍历列表即可知道选择了哪个位置。

      【讨论】:

      • 那么,在 onclicklistener 中,构建一个检查项目位置的数组列表?然后,根据该列表测试当前项目的位置。如果它在其中,继续行动吗?如果不是,我该如何重新循环?
      • 这个问题被问了很多,更多细节请看这里stackoverflow.com/a/2408987/1635817
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多