如何选择多个Checkbox并使用android

时间:2015-07-25 10:55:44

标签: android arrays checkbox

  

我已在我的CheckBox中添加了9 Fragment,我们正在接受检查   根据JSON的回应。如果用户想要更新他/她   个人资料,他/她可以check/uncheck任何一个。现在我必须确认一下   其中有多少是checked并创建Array作为   parameter据此而言。我没有使用任何adapter。   请让我们知道如何设置条件以获得唯一的值   选中复选框

我想这样做

cb_prfl_setng_general=(CheckBox)rootView.findViewById(R.id.cb_prfl_setng_general);
    cb_prfl_setng_economics = (CheckBox)rootView.findViewById(R.id.cb_prfl_setng_economics);
    cb_prfl_setng_business=(CheckBox)rootView.findViewById(R.id.cb_prfl_setng_business);
    cb_prfl_setng_social=(CheckBox)rootView.findViewById(R.id.cb_prfl_setng_social);
    cb_prfl_setng_politics=(CheckBox)rootView.findViewById(R.id.cb_prfl_setng_politics);
    cb_prfl_setng_entertainmnt=(CheckBox)rootView.findViewById(R.id.cb_prfl_setng_entertainmnt);
    cb_prfl_setng_technology=(CheckBox)rootView.findViewById(R.id.cb_prfl_setng_technology);
    cb_prfl_setng_spritual=(CheckBox)rootView.findViewById(R.id.cb_prfl_setng_spritual);
    cb_prfl_setng_sports=(CheckBox)rootView.findViewById(R.id.cb_prfl_setng_sports);



     if(cb_prfl_setng_general.isChecked()){

                String[] Interest = {"catagory_id=1"};

            }else if(cb_prfl_setng_economics.isChecked()){

                String[] Interest = {"catagory_id=1","catagory_id=2"};

            }else if(cb_prfl_setng_general.isChecked()){

                String[] Interest = {"catagory_id=1","catagory_id=2","catagory_id=3"};

            }else if(cb_prfl_setng_entertainmnt.isChecked()){

                String[] Interest = {"catagory_id=1","catagory_id=2","catagory_id=3","catagory_id=4"};
            }

但我不认为这是获得确切价值的正确方法。

2 个答案:

答案 0 :(得分:1)

您需要单独测试每个复选框并累积类别ID:

    List<String> interestList = new ArrayList<String>();

    // for each checkbox checked, accumulate a category id in the list
    if (cb_prfl_setng_general.isChecked()) {
        interestList.add("catagory_id=1");
    }

    if (cb_prfl_setng_economics.isChecked()) {
        interestList.add("catagory_id=2");
    }

    if (cb_prfl_setng_general.isChecked()) {
        interestList.add("catagory_id=3");
    }

    if (cb_prfl_setng_entertainmnt.isChecked()) {
        interestList.add("catagory_id=4");
    }

    .
    .
    .

    // convert the list into an array
    String[] interest = interestList.toArray(new String[interestList.size()]);

    System.out.println("interest");
    for (String str : interest) {
        System.out.println(str);
    }

答案 1 :(得分:0)

String WeekDays=""; List DayList = new ArrayList();

if (cbSunday.isChecked()) {
                DayList.add(String.valueOf(cbSunday.getText()));
            }
            if (cbMonday.isChecked()) {
                DayList.add(String.valueOf(cbMonday.getText()));
            }
            if (cbTuesday.isChecked()) {
                DayList.add(String.valueOf(cbTuesday.getText()));
            }
            if (cbWednesday.isChecked()) {
                DayList.add(String.valueOf(cbWednesday.getText()));
            }
            if (cbThursday.isChecked()) {
                DayList.add(String.valueOf(cbThursday.getText()));
            }
            if (cbFriday.isChecked()) {
                DayList.add(String.valueOf(cbFriday.getText()));
            }
            if (cbSaturday.isChecked()) {
                DayList.add(String.valueOf(cbSaturday.getText()));
            }


            String[] Days = DayList.toArray(new String[DayList.size()]);

            WeekDays="";

            for (String str : Days) {
                if(WeekDays.equals(""))
                {
                    WeekDays=WeekDays+str;
                }
                else {

                    WeekDays = WeekDays + "," + str;
                }

                WeekDays=WeekDays;                   
                 Log.d("LogDays",WeekDays);
                 
            }