【问题标题】:Add and Remove selected items of a checkbox from java ArrayList从 java ArrayList 添加和删除复选框的选定项目
【发布时间】:2018-12-30 05:45:57
【问题描述】:

我有一个函数可以根据 ArrayList 的值动态构建复选框,

现在我想在选中时将这些项目添加到另一个 ArrayList 并在取消选中时删除它们。

在选中项目时添加是有效的,但是当我取消选中复选框时,它会返回到以前的活动。

public void BuildCheckBox(){

    FinalSeatList.removeAll(FinalReservedSeatList);

    //Build checkboxus
    LinearLayout l1 = (LinearLayout)findViewById(R.id.linear_view);
    for(int i = 0; i < FinalSeatList.size(); i++) {
        final CheckBox cb = new CheckBox(this);
        cb.setText(FinalSeatList.get(i));
        l1.addView(cb);
        final int finalI = i;
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isChecked()) {
                    SelectedSeatList.add(FinalSeatList.get(finalI));
                    Toast.makeText(SelectSeatsActivity.this, "Added: " + SelectedSeatList.get(finalI), Toast.LENGTH_SHORT).show();
                }
                else
                {
                    if(SelectedSeatList.contains(FinalSeatList.get(finalI))){
                        SelectedSeatList.remove(FinalSeatList.get(finalI));
                        Toast.makeText(SelectSeatsActivity.this, "Removed: " + SelectedSeatList.get(finalI), Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(SelectSeatsActivity.this, "Unchecked", Toast.LENGTH_SHORT).show();
                    }


                }
            }

        });
    }




}

【问题讨论】:

  • 能否请您提及执行取消选中操作时得到的错误日志。
  • @Aniket 它在 logcat 上没有给出任何错误
  • 在你的问题描述中,你提到你得到一个错误......这是什么错误?
  • @Aniket 它只是回到以前的活动,我编辑了这个问题。对不起,我的错

标签: java android checkbox


【解决方案1】:

我自己解决了。

将 i 声明为 final 是问题所在,我使用 getText 方法设置了用于选择数组项的复选框,并且它起作用了。

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (buttonView.isChecked()) {
                        SelectedSeatList.add((String) cb.getText());
                        Toast.makeText(SelectSeatsActivity.this, "Added: " + (String) cb.getText(), Toast.LENGTH_SHORT).show();
                    }else{
                        if(SelectedSeatList.contains((String) cb.getText())){
                            SelectedSeatList.remove((String) cb.getText());
                            Toast.makeText(SelectSeatsActivity.this, "Removed: " + (String) cb.getText(), Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(SelectSeatsActivity.this, "Unchecked", Toast.LENGTH_SHORT).show();
                        }
                    }

                }

            });

【讨论】:

    【解决方案2】:

    像这样在你的活动中实现这个监听器 -

    public class ClassName extends Activity implements CompoundButton.OnCheckedChangeListener
    

    然后创建动态复选框-

    LinearLayout checkboxLayout = (LinearLayout)findViewById(R.id.chkboxlyt);
    
      for(int i=0;i<dynamicCheckBoxes.length;i++){
       CheckBox cb = new CheckBox(this);
       cb.setText("whaterever text);
       checkboxLayout.addView(cb);
       cb.setOnCheckedChangeListener(this);
    

    您将拥有此方法,因为您已经在活动中实现了侦听器。

    public void onCheckedChanged(CompoundButton cb, boolean isChecked){
      String checkedText = cb.getText()+"";
    
      if(isChecked){
       Toast.makeText(this, cb.getText()+" is checked!!!", Toast.LENGTH_SHORT).show();
      } else {
       // do remove operation here
      }
     }
    

    【讨论】:

      猜你喜欢
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多