2016-06-07 59 views
0

我正在测试应用程序,其中有20个选择题。我必须将选中的单选按钮值存储到一个字符串数组中,以便稍后在应用程序中使用用户选择的答案。以下是获取单选按钮文本的代码。现在我怎样才能将这20个选择保存到一个数组?将单选按钮选择保存为字符串数组

if (rg.getCheckedRadioButtonId() != -1) { 
       RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId()); 
       String ansText = uans.getText().toString(); 
} 
+0

有含20个疑问句或不同的屏幕不同的问题只有一个屏幕? – ManishMenaria

+0

@ManishMenaria它是一个屏幕我正在使用字符串阵列的问题和选项,用户必须点击下一步去下一个问题 –

+0

活动保持不变? – ManishMenaria

回答

1

,你可以将它们保存在字符串 的ArrayList中,其中问题的指标,也是阵列

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

    if (rg.getCheckedRadioButtonId() != -1) { 
     RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId()); 
     String ansText = uans.getText().toString(); 
     answersStringArray.add(ansText); 
    } 
0

你可以声明HashMap来保存一个

HashMap<String,String> hm=new HashMap<>(); 

使用hm.put(question_number,checked_answer);复选框值一个增加新的价值,HashMap类时,单选按钮被选中

使用hm.remove(question_number);从HashMap中删除值如果在检查后再次检查问题

在完成测验后,您可以像这样遍历HashMap

for(String key :hm.keySet()){ 
    System.out.println(hm.get(key)); 
    } 
0

你能得到quesText在答案的指数?

尝试使用HashMap

HashMap<String,String> mMap = new HashMap<>(); 
if (rg.getCheckedRadioButtonId() != -1) { 
    RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId()); 
    String ansText = uans.getText().toString(); 
    mMap.put(quesText,ansText); 
} 

如果你仅仅只保存答案,ü可以使用ArrayList

ArrayList<String> mArrayList = new ArrayList<>(); 
if (rg.getCheckedRadioButtonId() != -1) { 
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId()); 
String ansText = uans.getText().toString(); 
mArrayList.add(ansText); 
}