当我尝试在用户点击提交按钮时检查哪个radioButton时,日志始终打印-1表示结果(不是这种情况)。这使得感觉b / c每次buildQuestions()运行时我都会重置radioGroup。所以,我需要一种更好的方式。我尝试过v.getParent(),但这似乎并没有让我更接近。我的最终目标?要传递submitTask,选中的radioButton和它旁边的任何文本。我能做些什么不同的事情?
以下是构建整个UI元素的代码:
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);
}
这里是submitButton的onClickListener
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();
}
}
};