[英]How do i get the last radio button that was checked from a radio group in Android?
[英]I want to get the id of radio group by selecting the radio button of that group, i am creating both dynamically
我正在動態創建單選按鈕和無線電組,我想通過選擇該組的單選按鈕來獲取無線電組的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. }
});
嘗試做:v.getParent()。getId();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.