1

可能重复:
getCheckedRadioButtonId() 返回无用的 int?

当我尝试获取在用户点击提交按钮时检查了哪个单选按钮时,日志总是为结果打印-1(不是这种情况)。这很有意义,因为每次 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();

        }
    }   
};
4

1 回答 1

-1

在这里解决:

https://stackoverflow.com/a/10356828/1231943

于 2012-04-27T20:03:01.520 回答