如何选择多个复选框,并创建Android中的数值数组 [英] How to select multiple Checkbox and create an Array with the values in android

查看:199
本文介绍了如何选择多个复选框,并创建Android中的数值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了9 复选框在我的这是越来越检查片段
  根据来自JSON的响应。如果用户要更新他/她的
  个人主页上,他/她可以选中/清除任何人。现在,我必须确认
  这其中有多少是检查并创建一个阵列作为
  根据该参数。我没有使用任何适配器为。
  请让知道如何设置的条件来获得唯一的价值
  选中的复选框

I have added 9 CheckBox in my Fragment which is getting checked according to the response from JSON. If user wants to update his/her profile, He/she can check/uncheck any of them. Now I have to confirm that how many of them are checked and create an Array as a parameter according to that. I am not using any adapter for that. please let know how to set the condition to get the value of only checked checkboxes

我试图做这样的

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

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

But I don't think this is the right way to get the exact value.

推荐答案

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

You need to test each checkbox separately and accumulate the category ids:

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

这篇关于如何选择多个复选框,并创建Android中的数值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆