Reputation: 397
I am working on quiz application in android. We have created Select.java page which displays the questions and options(with radio buttons) from sqlite database. Also we created a header.java file for displaying buttons i.e back and next buttons for the Select.java page.
Here we need to get the selected radio button id and need to send that to Header class. Because header class consists of the next button onclick action. Once the next button is clicked the selected radio button value has to be stored in arraylist. We created radio buttons in Select.java class. So my question is how to get the selected radio button id into that next button click action. Please help me regarding this.
Thanks in Advance.
Upvotes: 6
Views: 38147
Reputation: 328
In RadioGroup Class you can use method getCheckedRadioButtonId();
RadioGroup rg = findViewById(R.id.radioGroup);
rg.getCheckedRadioButtonId();
Returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1
.
Upvotes: 2
Reputation: 760
I know it's a old question but i don't see my answer in anywhere and i found it more simple than others..
so here we go:
int myRadioChecked;
if(radioGroup.getCheckedRadioButtonId() == findViewById(R.id.YOUR_RADIO_BUTTON).getId()) {
/**Do Stuff*/
//ex.: myRadioChecked = 1;
}
Upvotes: 5
Reputation: 500
you can get the id of selected button by the following this.Here
int position = group.indexOfChild(radioButton);
will give you the id.Also you can to Toast to see id like this
Toast.makeText(MainActivity.this,"Id of radio button"+position+, Toast.LENGTH_SHORT).show();
This will pop up - "Id of radio button you clicked is 0" if you clicked first button.
Upvotes: 0
Reputation: 1148
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.MyRadioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = radioGroup.getCheckedRadioButtonId();
Log.i("ID", String.valueOf(selectedId));
}
});
Upvotes: 2
Reputation: 3619
Hmmm, just add one more member variable in UserBO to store selected answer.
Class UserBO {
private int userID;
private String userName;
private String question;
private String option1;
private String option2;
private String option3;
private int answerID;
//create getter and setters for above member variables
}
then within onclick listener of Adapter class, do like as following
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup,
int radioButtonID) {
switch(radioButtonID) {
case R.id.option1:
listItem.setAnswerID(1);
break;
case R.id.option2:
listItem.setAnswerID(2);
break;
}
}
});
then change your header constructor to receive userarraylist (which contains user details with answer)
ArrayList<USerBO> userList;
Header(Context context, AttributeSet attrs, ArrayList<UserBO> userALt) {
userList = userAL;
}
//on next button click
onclick() {
for(UserBO userObj: userList) {
if (userObj.getAnswerID != 0)
Log.d("AnswerID", userObj.getAnswerID);
}
}
it just like sudo code.. i hope this will help u..
Upvotes: 0
Reputation: 2403
Your layout xml file should be like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:orientation="vertical"
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/option1"
android:text="Option1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/option2"
android:text="Option2"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/option3"
android:text="Option3"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/option4"
android:text="Option4"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/option5"
android:text="Option5"
/>
</RadioGroup>
</LinearLayout>
Add the below ode in your Activity
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton) findViewById(checkedId);
String text = checkedRadioButton.getText().toString();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
Upvotes: 8