Reputation: 4082
I am developing a contest ( multiple choice type ) in android with server database. Had many questions with multiple choice answers ( 4 per each ). Creating the questions by the following code
try {
JSONArray array = (JSONArray) new JSONTokener(questions).nextValue();
TextView active=(TextView)findViewById(R.id.active);
active.setText(description);
active.setVisibility(View.VISIBLE);
LinearLayout ll=(LinearLayout)findViewById(R.id.questions_lay);
for(int i=0;i<array.length();i++) {
JSONObject question = array.getJSONObject(i);
Integer id=question.getInt("id");
TextView title = new TextView(this);
title.setText(question.getString("ques"));
title.setTextColor(Color.parseColor("#FFFFFF"));
ll.addView(title);
final RadioButton[] rb = new RadioButton[4];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
rb[0] = new RadioButton(this);
rb[0].setText(question.getString("opt_a"));
rb[1] = new RadioButton(this);
rb[1].setText(question.getString("opt_b"));
rb[2] = new RadioButton(this);
rb[2].setText(question.getString("opt_c"));
rb[3] = new RadioButton(this);
rb[3].setText(question.getString("opt_d"));
rg.addView(rb[0]);
rg.addView(rb[1]);
rg.addView(rb[2]);
rg.addView(rb[3]);
ll.addView(rg);
}
ll.setVisibility(View.VISIBLE);
overLay.setVisibility(View.GONE);
} catch (JSONException e) {
// handle JSON parsing exceptions...
Toast.makeText(ContestActivity.this,"Error Occured ! Please try again.",Toast.LENGTH_SHORT).show();
cd.goHome(ContestActivity.this);
e.printStackTrace();
}
Now on a button click I need to send the answers to the server. I am new to android and my doubts are
Thanks in advance
Upvotes: 0
Views: 3171
Reputation: 384
ok first create a button in your layout :
1) your Question goes here ?
Item 4
Click me Button
This is your layout:
now if you want to set some values to radio button then set it via setTag or setId and then in code :
final RadioButton[] rb = new RadioButton[4];
rb[1].setTag("some value");
rb[1].setId(1);
int arrayQID[] = new Int[array.length()];
and then on click listener of your button
int selectedId = rg.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) layout.findViewById(selectedId);
String value = (String)radioButton.getTag();
int value2 = radioButton.getId();
for(int i=0;i<array.length();i++) {
JSONObject question = array.getJSONObject(i);
Integer id=question.getInt("id");
TextView title = new TextView(this);
title.setText(question.getString("ques"));
title.setTextColor(Color.parseColor("#FFFFFF"));
ll.addView(title);
final RadioButton[] rb = new RadioButton[4];
RadioGroup rg = new RadioGroup(this);
rg.setId(qid); // i suggest set your question id here instead of loop count
rg.setTag("some value"); // Some value
arrayQID[i] = qid
rg.setOrientation(RadioGroup.VERTICAL);
rb[0] = new RadioButton(this);
rb[0].setText(question.getString("opt_a"));
rb.[0].setId(id); //id should be unique (not the same for each radioGroup [1]
rb.[0].setTag("qid"+"opt_a");
rb[1] = new RadioButton(this);
rb[1].setText(question.getString("opt_b"));
rb[2] = new RadioButton(this);
rb[2].setText(question.getString("opt_c"));
rb[3] = new RadioButton(this);
rb[3].setText(question.getString("opt_d"));
rg.addView(rb[0]);
rg.addView(rb[1]);
rg.addView(rb[2]);
rg.addView(rb[3]);
ll.addView(rg);
}
[1] here you can built your logic by combining the question id and option id ie (qid + opt_a_id) = id and decode it where gid = radioButtonid - opt_a_id something like this in shot you make an id which is unique and can be decoded so that you know that this radioButton belongs to this question Similarly you can use tags to get the view object with the help of findViewWithTag("qid"+"opt_a"); you can do this
RadioButton rb = layout.findViewWithTag("qid"+"opt_a");
and to get all question and answer you can set foreach loop:
for(int qid : arrayQID) {
RadioButton rb = layout.findViewWithTag("qid"+"opt_a"); // OR
RadioButton rb = layout.findViewById(id); // Unique id ie (qid + opt_a_Id)
}
Upvotes: 1