How to get verified radio button id created dynamically in android

In my activity I create a dynamic radio group. I have some questions and answers in the Sqlite database, loading them in action, and then setting up in RadioGroup. This is working fine. But after that I want to get all the identifiers of the selected switch by the user in order to save them in the database. In general, we do something similar when we have an option ID:

            id1=rdgroup1.getCheckedRadioButtonId();
            que1=(RadioButton)findViewById(id1);
            ans1=que1.getId()+""; // so here I will get radio button ID.

So my questions are: how do I get the selected button id. Here is my code for dynamically creating a radio group.

     LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
        c1=db.rawQuery("SELECT * FROM QueTable WHERE AgeGroup='10-20'", null);
        c1.moveToFirst();
        int i = c1.getCount();
        if(i > 0)
        {
            Log.w("START", "start");
            while (i > 0)
            {
                TextView title = new TextView(this);
                questions = c1.getString(1);
                title.setText(questions);
                title.setTextColor(Color.BLACK);
                mLinearLayout.addView(title);
                // create radio button

                answers=c1.getString(2);
                String[] answer = answers.split(",");
                rb = new RadioButton[5];
                rg = new RadioGroup(this);
                rg.setOrientation(RadioGroup.VERTICAL);

                int k = answer.length;

                for (int j = 0; j < k; j++)
                {
                    rb[j] = new RadioButton(this);
                    rg.addView(rb[j]);
                    rb[j].setText(answer[j]);
                }
                mLinearLayout.addView(rg);
                c1.moveToNext();
                i--;
            }
        }
+4
source share
3 answers

... - . , . :

private static final int RB_ID = 100;
int k=0,len=0,r=0,p = 0,q=0, len = 0;

 LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
 c1 = db.rawQuery("SELECT * FROM QueMotion WHERE AgeGroup ='"+Age+"' LIMIT 5", null);
            c1.moveToFirst();
            int i = c1.getCount();
            rg = new RadioGroup[i];
            rb = new RadioButton[i*5];
            if (i > 0) {
                Log.w("START", "start");
                while (i > 0)
                {
                    TextView title = new TextView(this);
                    questions = c1.getString(1);// retrive from database
                    title.setText(questions);
                    title.setTextColor(Color.BLACK);
                    title.setTypeface(null, Typeface.BOLD);
                    title.setPadding(0, 0, 0, 10);
                    mLinearLayout.addView(title);
                    answers = c1.getString(2);// retrive from database
                    String[] answer = answers.split(",");// will create options for radio button.
                    rg[p] = new RadioGroup(this);
                    rg[p].setOrientation(RadioGroup.VERTICAL);
                    len=len+answer.length;
                    for (int j = 0; j < answer.length; j++)
                    {
                            rb[q] = new RadioButton(this);
                            rg[p].addView(rb[q]);
                            rb[q].setId(k + RB_ID);
                            rb[q].setText(answer[j]);
                            k++;
                            q++;
                    }
                    mLinearLayout.addView(rg[p]);
                    c1.moveToNext();
                    i--;
                    p++;
                }

    //Submit button click
     submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (rg[0].getCheckedRadioButtonId() == -1)
                {
                    Log.e("Nothing selected", "Nothing selected");
                }
                else
                {
                    for (r = 0; r < len; r++) {
                        if (rb[r].isChecked()) {
                            RadioButton id = (RadioButton) findViewById(rb[r].getId());
                            String radioText = id.getText().toString();
                            c3.moveToFirst();
                            Log.e("RadioString", radioText);
                        } else {
                            Log.e("RadioString", "nothing");
                        }
                    }
                }
            }
        });
+2

RadioButton, findViewById, .

RadioButton, View.setId(). :

. . .

, , findViewById , View.generateViewId, API 17.

API-, , () ID. Android: View.setID(int id) - ?

+2

, RadioButton Id

  for (int j = 0; j < k; j++)
            {
               RadioButton rb = new RadioButton(this);
                rb.setId(Somenumber + j)
                 rb[j] = rb;
                rg.addView(rb[j]);
                rb[j].setText(answer[j]);
            }

, setId, , , .

                rb.setTag(SomeObject)
+1

All Articles