我正在动态创建单选按钮和单选组,我想通过选择该组的单选按钮来获取单选组的ID,名称。我正在动态创建单选组和单选按钮。
String cat_mname= getIntent().getStringExtra("cat_name");
String Radio_button_loop= getIntent().getStringExtra("Radio_button_loop");
int Radio_button_to_int = Integer.parseInt(Radio_button_loop);
Toast.makeText(getApplicationContext(),Radio_button_loop,Toast.LENGTH_LONG).show();
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(cat_mname);
mlinearLayout = (LinearLayout) findViewById(R.id.values);
Cursor cursor = dbController.getdata("SELECT * FROM DATA4 WHERE catname = '"+cat_mname+"'");
arrayList.clear();
while (cursor.moveToNext())
{
int id = cursor.getInt(0);
String cat_name = cursor.getString(1);
String cat_option_value = cursor.getString(2);
String cat_type = cursor.getString(3);
String value = cursor.getString(4);
arrayList.add(new data_class(id,cat_name,cat_option_value,cat_type,value));
}
String aa[] = {"X","O","S","H"};
these are radio button values for radio group
for (int k = 0; k < arrayList.size(); k++) {
//create text button
TextView title = new TextView(getApplicationContext());
title.setText(arrayList.get(k).getCat_name());
setting title
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
title.setTextColor(Color.RED);
mlinearLayout.addView(title);
// create radio button
rb = new RadioButton(this);
rg = new RadioGroup(getApplicationContext());
rg.setId(arrayList.get(k).getId());
here i am setting the id or text of radio group so i can get it.
rg.setOrientation(RadioGroup.HORIZONTAL);
for (int i = 0; i < Radio_button_to_int; i++) {
// in this loop, i am creating radio buttons dynamically for radio groups.
rb = new RadioButton(getApplicationContext());
rg.addView(rb);
rb.setText(aa[i]);
here i am creating radio buttons for radiogroup
}
mlinearLayout.addView(rg);
}
rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
here i want to get radio group value that i set above but its not returning. }
});
答案 0 :(得分:0)
尝试做: v.getParent()。getId();