Possible Duplicate:
getCheckedRadioButtonId() returning useless int?
When I try to get which radioButton was checked when the user taps the submit button, the Log always prints -1 for a result (not the case). This makes senses b/c I reset radioGroup each time buildQuestions() runs. So, I need a way that’s better. I’ve tried v.getParent(), but that doesn’t seem to get me closer. My end goal? To pass along to submitTask, the selected radioButton and whatever text is next to it. What can I do differently?
Here’s the code that builds the whole UI element:
public class ElectionsFragment extends SherlockFragment {
TableLayout questionContainer;
View mainLayout;
int leftMargin=0;
int topMargin=4;
int rightMargin=0;
int bottomMargin=0;
RadioGroup radioGroup;
Polling activity;
public void buildQuestions(JSONObject question) throws JSONException {
LayoutInflater inflater = (LayoutInflater) getActivity().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//progressBar.setVisibility(View.GONE);
questionContainer = (TableLayout) mainLayout.findViewById(R.id.questionContainer);
//questionContainer.removeAllViews();
View questionBox = inflater.inflate(R.layout.question, null);
questionBox.setId(Integer.parseInt(question.getString("id")));
TextView title = (TextView) questionBox.findViewById(R.id.questionTextView);
title.setText(question.getString("title"));
radioGroup = (RadioGroup) questionBox.findViewById(R.id.responseRadioGroup);
String typeFromTable = question.getString("default");
Log.v("default", typeFromTable);
int responseType = Integer.parseInt(typeFromTable);
if (responseType == 1) {
Log.v("1", question.toString());
//populate default radio buttons
Resources res = getResources();
String[] defaultAnswers = res.getStringArray(R.array.defaultAnswers);
int j = 0;
while (j < 5) {
RadioButton rb = new RadioButton(getActivity());
rb.setText(defaultAnswers[j]);
radioGroup.addView(rb);
j++;
}
}
else if (responseType == 0) {
for (int j = 0; j < 5; j++) {
if (question.getString("answer" + Integer.toString(j)) != "null") {
RadioButton rb;
rb = new RadioButton(getActivity());
rb.setText(question.getString("answer" + Integer.toString(j)));
if (question.getString("answer" + Integer.toString(j)).length() != 0) {
radioGroup.addView(rb);
}
}
else {
if (question.getString("answer" + Integer.toString(j)) == "null") {
RadioButton rb;
rb = new RadioButton(getActivity());
rb.setText("null");
radioGroup.addView(rb);
}
}
}
}
Button chartsButton = (Button) questionBox.findViewById(R.id.chartsButton);
chartsButton.setTag(question);
Button submitButton = (Button) questionBox.findViewById(R.id.submitButton);
chartsButton.setOnClickListener(chartsListener);
submitButton.setOnClickListener(submitListener);
TableRow tr = (TableRow) questionBox;
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT);
trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trParams);
questionContainer.addView(tr);
submitButton.setTag(question);
}
And here’s the onClickListener for submitButton
public OnClickListener submitListener = new OnClickListener() {
public void onClick(View v) {
userFunctions = new UserFunctions();
if (userFunctions.isUserLoggedIn(activity)) {
Log.v("submit", Integer.toString(radioGroup.getCheckedRadioButtonId()));
SubmitTask submitTask = new SubmitTask((Polling) activity, (JSONObject) v.getTag());
submitTask.execute();
}
}
};
Solved here:
https://stackoverflow.com/a/10356828/1231943