如何从programatic创建的复选框中删除值?

时间:2018-04-17 12:03:03

标签: java android

该应用程序是关于使用复选框进行简单调查。复选框值来自REST Api,如[" option1"," option2" ...]

我会将选中的复选框也作为列表发布。所有设置和工作除了删除未选择的项目。如何获取和删除未选中的'列表中的数据? (用户可以进行更改)我尝试使用List和HashMap,无法实现。提前谢谢!

public class MainActivity extends AppCompatActivity {

LinearLayout linear_checkbox;
List<String> answerList= new ArrayList<>();
List<String> checkedList = new ArrayList<>();

Map<String,String> map = new HashMap<>();

Button button_send;

int j;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //
    linear_checkbox = (LinearLayout)findViewById(R.id.linear_checkbox);
    button_send= (Button)findViewById(R.id.button_send);
    //

    answerList.add("Photography");
    answerList.add("Music");
    answerList.add("Skateboard");

    listChoices(cevapList);

    button_send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Toast.makeText(MainActivity.this, "LIST: "+"\n"+ checkedList.toString(), Toast.LENGTH_SHORT).show();
            //Toast.makeText(MainActivity.this, ""+map.toString(), Toast.LENGTH_SHORT).show();
        }
    });

}

public void listChoices(List<String> textList){

    textList = answerList;

    for(j=0; j<textList.size(); j++){
        final CheckBox cb = new CheckBox(getApplicationContext());
        cb.setText(textList.get(j));
        cb.setTextColor(Color.parseColor("#E7740D"));
        linear_checkbox.addView(cb);

        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {


                if(b==true){
                    Toast.makeText(MainActivity.this, ""+cb.getText()+" selected", Toast.LENGTH_SHORT).show();
                    String key = String.valueOf(j);
                    //map.put(key,cb.getText().toString());
                    secilenlerList.add(cb.getText().toString());
                }else{
                    Toast.makeText(MainActivity.this, ""+cb.getText()+" unselected", Toast.LENGTH_SHORT).show();

                }

            }
        });
    }

}

}

1 个答案:

答案 0 :(得分:0)

更新您的代码工作正常。当用户取消选中任何复选框项时,您没有进行更改。

 public class MainActivity extends AppCompatActivity {

    LinearLayout linear_checkbox;
    List<String> answerList = new ArrayList<>();
    List<String> checkedList = new ArrayList<>();

    Map<String, String> map = new HashMap<>();

    Button button_send;

    int j;
    private ArrayList<String> secilenlerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //
        linear_checkbox = (LinearLayout) findViewById(R.id.linear_checkbox);
        button_send = (Button) findViewById(R.id.button_send);
        //

        answerList.add("Photography");
        answerList.add("Music");
        answerList.add("Skateboard");

        listChoices(answerList);

        button_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "LIST: " + "\n" + secilenlerList.toString(), Toast.LENGTH_SHORT).show();
                //Toast.makeText(MainActivity.this, ""+map.toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void listChoices(List<String> textList) {
        textList = answerList;
        secilenlerList = new ArrayList<>();

        for (j = 0; j < textList.size(); j++) {
            final CheckBox cb = new CheckBox(getApplicationContext());
            cb.setText(textList.get(j));
            cb.setTextColor(Color.parseColor("#E7740D"));
            linear_checkbox.addView(cb);

            cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean ischecked) {


                    if (ischecked) {
                        Toast.makeText(MainActivity.this, "" + cb.getText() + " selected", Toast.LENGTH_SHORT).show();
                        String key = String.valueOf(j);
                        //map.put(key,cb.getText().toString());
                        if (!secilenlerList.contains(cb.getText().toString()))
                            secilenlerList.add(cb.getText().toString());
                    } else {
                        if (secilenlerList.contains(cb.getText().toString()))
                            secilenlerList.remove(cb.getText().toString());
                        Toast.makeText(MainActivity.this, "" + cb.getText() + " unselected", Toast.LENGTH_SHORT).show();

                    }

                }
            });
        }

    }
}