Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7857907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:11:29+00:00 2026-06-02T21:11:29+00:00

Possible Duplicate: getCheckedRadioButtonId() returning useless int? When I try to get which radioButton was

  • 0

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();

        }
    }   
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:11:31+00:00Added an answer on June 2, 2026 at 9:11 pm

    Solved here:

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Unable to get a list of installed Python modules How do I
Possible Duplicate: Why do I get a null pointer exception from TabWidget? I have
Possible Duplicate: How to store an IP in mySQL I want to get the
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How to get all the pictures from the sdcard of emulator and
Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.