如何在ArrayList中选中的复选框值存储在机器人?

如何在ArrayList中选中的复选框值存储在机器人?

问题描述:

我工作的测验应用即可。对于每个问题有多个答案并所以,我已经使用的复选框即可。在每个页面的我显示与选择简单的问题将会显示出来,点击下一步按钮时,下一个问题。 所以当多个答案由我需要将它们存储在一个ArrayList和需要选择的答案与数据库比较用户选择的。在我的code我保存所选的值id的One由一个,但在同一时间它仅存储一个值。如何存储所有选中的复选框的ID在我的ArrayList?

I am working on quiz application. For each question there are multiple answers and so for that I have used checkboxes. In each page I am displaying single question with options and when next button is clicked next question will be displayed. So when multiple answers are selected by the user I need to store them in an arraylist and need to compare the selected answers with that in database. In my code I am storing the selected value id's one by one but at a time it is storing only one value. How to store all the selected checkbox id's in my arraylist?

我的code:

ArrayList<String> selchkboxlist;
CheckBox[] cbs;
 cbs = new CheckBox[20];
for(k=0; k<List3.size(); k++)
 { 
     arr = List3.get(k);
     cbs[k] = new CheckBox(getContext());
     Rl.addView(cbs[k]);
         cbs[k].setText((CharSequence) arr.get(2));
         cbs[k].setTextColor(Color.parseColor("#000000"));  
         cbs[k].setId(k);
         cbs[k].setOnClickListener( new View.OnClickListener()
         { 
             public void onClick(View v) 
             { 
                CheckBox cb = (CheckBox) v ; 
            if (((CheckBox) v).isChecked()) {
                chk = Integer.toString(v.getId());
                    selchkboxlist = new ArrayList<String>();
                    selchkboxlist.add(chk);
                    Toast.makeText(Select1.this, "Selected CheckBox ID" + v.getId(), Toast.LENGTH_SHORT).show();
            } else {
                 selchkboxlist.remove(chk);
              Toast.makeText(Select1.this, "Not selected", Toast.LENGTH_SHORT).show();
                }
         }
     });
 } 

请帮我关于这个....在此先感谢

Please help me regarding this....Thanks in Advance

在初始化selchkboxlist for循环....

Initialize the selchkboxlist before for loop....

ArrayList<String> selchkboxlist=new ArrayList<String>();
CheckBox[] cbs;
cbs = new CheckBox[20];
for(k=0; k<List3.size(); k++)
{ 
 arr = List3.get(k);
 cbs[k] = new CheckBox(getContext());
 Rl.addView(cbs[k]);
     cbs[k].setText((CharSequence) arr.get(2));
     cbs[k].setTextColor(Color.parseColor("#000000"));  
     cbs[k].setId(k);
     cbs[k].setOnClickListener( new View.OnClickListener()
     { 
         public void onClick(View v) 
         { 
            CheckBox cb = (CheckBox) v ; 
        if (((CheckBox) v).isChecked()) {
            chk = Integer.toString(v.getId());
                selchkboxlist.add(chk);
                Toast.makeText(Select1.this, "Selected CheckBox ID" + v.getId(), Toast.LENGTH_SHORT).show();
        } else {
             selchkboxlist.remove(chk);
          Toast.makeText(Select1.this, "Not selected", Toast.LENGTH_SHORT).show();
            }
     }
 });

}