2016-05-12 87 views
0

我想根据radio组的checkedId在ArrayList“arr”中添加数据。我有4个单选按钮“a0,a1,a2,a3”。所以如果我选择a1,数组列表应该添加rb2的值。并选择A1后,如果我选择A2然后在“ARR”以前的值应该得到更新(没有得到的第一个后加入)等on.Any帮助..数据没有被添加到radiolroup的数组列表中?

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 

      ArrayList<String> arr = new ArrayList<String>(); 

      for (int i = 0; i <= mcq.size(); i++) { 
       switch (checkedId) { 
        case R.id.a0: 
         // do operations specific to this selection 

         Toast.makeText(getApplication(), 
           rb1.getText(), Toast.LENGTH_SHORT).show(); 
         break; 

        case R.id.a1: 
         // do operations specific to this selection 

         Toast.makeText(getApplication(), 
           rb2.getText(), Toast.LENGTH_SHORT).show(); 
         break; 

        case R.id.a2: 
         // do operations specific to this selection 

         Toast.makeText(getApplication(), 
           rb3.getText(), Toast.LENGTH_SHORT).show(); 
         break; 
        case R.id.a3: 
         // do operations specific to this selection 

         Toast.makeText(getApplication(), 
           rb4.getText(), Toast.LENGTH_SHORT).show(); 
         break; 

       } 
       arr.add(String.valueOf(checkedId)); 
       Log.e("", String.valueOf(arr)); 
      } 


     } 
    }); 

回答

0

您可以使用https://developer.android.com/reference/android/util/SparseArray.html

SparaseArray<Boolean> array = new SparseArray(); 
array.put(checkedId, rb.isChecked()); 

然后你就可以得到阵列这样所有选中的元素:

for(int i = 0; i< sparseArray.size(); i++) { 
    int id = sparseArray.keyAt(i); 
    Boolean isChecked = sparseArray.get(id); 
    if(isChecked) { 
     log("Identifier " + id + " is checked"); 
    } 
} 
+0

我得到“无法解析符号”。做我必须首先确定其大小@ AlexanderKulyakhtin –