如何选择多个复选框,并创建Android中的数值数组 [英] How to select multiple Checkbox and create an Array with the values in android
问题描述
我添加了9 复选框
在我的这是越来越检查片段
根据来自JSON的响应。如果用户要更新他/她的
个人主页上,他/她可以选中/清除
任何人。现在,我必须确认
这其中有多少是检查
并创建一个阵列
作为
根据该参数
。我没有使用任何适配器
为。
请让知道如何设置的条件来获得唯一的价值
选中的复选框
I have added 9
CheckBox
in myFragment
which is getting checked according to the response from JSON. If user wants to update his/her profile, He/she cancheck/uncheck
any of them. Now I have to confirm that how many of them arechecked
and create anArray
as aparameter
according to that. I am not using anyadapter
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屋!