简体   繁体   English

如何获取在android中动态创建的单选按钮的id

[英]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. 我在Sqlite数据库中有一些问题和答案,在活动中检索它们然后在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. 但在那之后,我希望用户获得所选的单选按钮的所有ID以将它们存储在数据库中。 In general, we do something like this when we have option's ID : 一般来说,当我们有选项的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. 所以我的问题是,我将如何获得所选单选按钮的ID。 Here's my code for dynamically creating radiogroup. 这是我动态创建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--;
            }
        }

The place where you create RadioButton set and Id to it 您创建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]);
            }

Like this you can setId or also if you want you can add tag to process. 像这样你可以setId或者如果你想要你可以添加标签来处理。

                rb.setTag(SomeObject)

You need to set an ID for your programmatically created RadioButton in order to find it using findViewById as you show in your first code. 您需要为以编程方式创建的RadioButton设置ID,以便在第一个代码中显示时使用findViewById找到它。

To set the ID for your RadioButton , you can use View.setId() . 要设置RadioButton的ID,可以使用View.setId() The documentation says: 文件说:

Sets the identifier for this view. 设置此视图的标识符。 The identifier does not have to be unique in this view's hierarchy. 标识符在此视图的层次结构中不必是唯一的。 The identifier should be a positive number. 标识符应为正数。

The identifier does not have to be unique, but if it's not unique, findViewById will return the first occurence, so you can use the static method View.generateViewId added on API level 17. 标识符不必是唯一的,但如果它不是唯一的,则findViewById将返回第一次出现,因此您可以使用在API级别17上添加的静态方法View.generateViewId

If you are using lower APIs, you may want to write your own function to find a suitable (unused) ID. 如果您使用的是较低的API,则可能需要编写自己的函数来查找合适的(未使用的)ID。 You can take a look at Android: View.setID(int id) programmatically - how to avoid ID conflicts? 您可以通过编程方式查看Android:View.setID(int id) - 如何避免ID冲突?

Ok..So finally I got the solution. 好的......最后我得到了解决方案。 I am going to share complete code here that will create dynamically radiogroup and radiobuttons from database. 我将在这里分享完整的代码,它将从数据库中创建动态radiogroup和radiobuttons。 Here is code below : 这是下面的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM