【发布时间】:2015-05-14 07:18:18
【问题描述】:
在我的活动中,我正在创建一个动态无线电组。我在 Sqlite 数据库中有一些问题和答案,在活动中检索它们,然后在 RadioGroup 中设置。这工作正常。但在那之后,我想通过用户获取所选单选按钮的所有 ID 以将它们存储在数据库中。一般来说,当我们有选项的 ID 时,我们会这样做:
id1=rdgroup1.getCheckedRadioButtonId();
que1=(RadioButton)findViewById(id1);
ans1=que1.getId()+""; // so here I will get radio button's ID.
所以我的问题是,我将如何获得选定单选按钮的 ID。这是我动态创建无线电组的代码。
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--;
}
}
【问题讨论】:
-
how will I get selected radio button's ID但您正在使用que1.getId()获取 id 那么有什么问题? -
@ρяσѕρєяK 问题是我正在以编程方式创建无线电组。所以我不知道任何单选按钮的 ID。请参阅动态无线电组代码的第二部分。当我们在 xml 布局中使用拖放创建它们时使用 que1.getId(),然后在 xml 中为它们提供 id,如 android:id...
-
那么有什么问题使用
id1=rg.getCheckedRadioButtonId();que1=(RadioButton)findViewById(id1);ans1=que1.getId()+"";?? -
@ρяσѕρєяK 无论你说什么都是静态单选按钮而不是动态。所以我知道那个静态单选按钮的 ID,但不知道动态添加的按钮。
-
@Ruchir Tarawat : 您可以在代码中使用 setid 方法设置单选按钮 id
标签: java android sqlite android-sqlite android-radiogroup