Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get checked radio button's id created dynamically in android

In my activity, I am creating a dynamic radiogroup. I have some questions and answers in Sqlite database, retrieving them in activity and then set in RadioGroup. This is working fine. But after that, I want to get all ID of selected radio button by user to store them in database. In general, we do something like this when we have option's ID :

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

So my questions is, how will I get selected radio button's ID. Here's my code for dynamically creating radiogroup.

     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--;
            }
        }
like image 434
Ruchir Tarawat Avatar asked Mar 12 '15 08:03

Ruchir Tarawat


1 Answers

The place where you create RadioButton set and Id to it

  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]);
            }

Like this you can setId or also if you want you can add tag to process.

                rb.setTag(SomeObject)
like image 161
Lochana Ragupathy Avatar answered Sep 28 '22 18:09

Lochana Ragupathy