[英]How to get checked radio button and how to control only one radio button selected in list view
I trying to add my data to a list view.我试图将我的数据添加到列表视图中。 I am using adapter on this.我正在使用适配器。 Inside the list view have radio button and some field.在列表视图中有单选按钮和一些字段。
class StocktakeEditViewAdaptor : BaseAdapter<Model.FileRecord>
{
private Dictionary<int, bool> checkDictionary = new Dictionary<int, bool>();
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
//if (view == null)
{
view = context.LayoutInflater.Inflate(Resource.Layout.StockTakeEditDetailList, null);
view.DuplicateParentStateEnabled = true;
createdview.Add(view);
RadioButton lblradio = view.FindViewById<RadioButton>(Resource.Id.lblradio);
lblradio.Tag = item.FileRecord_ID + ":" + item.ST_filename + ":" + item.ST_BinLoc;
lblradio.Checked = checkDictionary[position]; //add position here
lblradio.SetOnCheckedChangeListener(null);
lblradio.SetOnCheckedChangeListener(new CheckedChangeListener(this.context));
view.FindViewById<TextView>(Resource.Id.txtLineNo).Text = item.FileRecord_ID.ToString(); //my field in adapter.
view.FindViewById<TextView>(Resource.Id.txtbinloc).Text = item.ST_BinLoc.ToString();
view.FindViewById<TextView>(Resource.Id.txtBarcodett).Text = item.ST_Barcode.ToString();
view.FindViewById<TextView>(Resource.Id.txtQtytt).Text = item.ST_Qty.ToString();
if (!view.HasOnClickListeners)
view.Click += View_LongClick;
view.RefreshDrawableState();
}
return view;
}
public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
for (int i = 0; i < checkDictionary.Count(); i++)
{
if (i == (int)buttonView.Tag)
{
checkDictionary[i] = true; //error on here
}
else
{
checkDictionary[i] = false; //error on here
}
}
}
I able to show my record in list view.But the radio button can select multiple.我可以在列表视图中显示我的记录。但是单选按钮可以选择多个。 I do not want select multiple record.我不想选择多条记录。
You could create a collection to store the check status of the radiobutton, and then load the collection when it is loaded in getView
.您可以创建一个集合来存储单选按钮的检查状态,然后在getView
加载该集合时加载该集合。 like this :像这样 :
class YourAdapter : BaseAdapter,CompoundButton.IOnCheckedChangeListener
{
private Dictionary<int, bool> checkDictionary = new Dictionary<int, bool>();
int[] item; //raplace your own data
public MyAdapter(int[] value) //raplace your own data
{
item = value;
for (int i = 0; i < item.Length; i++)
{
checkDictionary.Add(i,false);
}
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
//if (view == null)
{
view = context.LayoutInflater.Inflate(Resource.Layout.StockTakeEditDetailList, null);
view.DuplicateParentStateEnabled = true;
createdview.Add(view);
RadioButton lblradio = view.FindViewById<RadioButton>(Resource.Id.lblradio);
lblradio.Tag = position;
lblradio.Checked = checkDictionary[position];
lblradio.SetOnCheckedChangeListener(this);
view.FindViewById<TextView>(Resource.Id.txtLineNo).Text = item.FileRecord_ID.ToString(); //my field in adapter.
view.FindViewById<TextView>(Resource.Id.txtbinloc).Text = item.ST_BinLoc.ToString();
view.FindViewById<TextView>(Resource.Id.txtBarcodett).Text = item.ST_Barcode.ToString();
view.FindViewById<TextView>(Resource.Id.txtQtytt).Text = item.ST_Qty.ToString();
if (!view.HasOnClickListeners)
view.Click += View_LongClick;
view.RefreshDrawableState();
}
return view;
}
public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
{
for (int i = 0; i < checkDictionary.Count; i++)
{
if (i == (int) buttonView.Tag)
{
checkDictionary[i] = true;
}
else
{
checkDictionary[i] = false;
}
}
NotifyDataSetChanged();
}
}
The reason you can select multiple radio button is that they do not belong to the same Radio button group.您可以选择多个单选按钮的原因是它们不属于同一个单选按钮组。
A quote from the official document of Android Radiobutton .引用自 Android Radiobutton的官方文档。
To create each radio button option, create a RadioButton in your layout.要创建每个单选按钮选项,请在您的布局中创建一个 RadioButton。 However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup.但是,因为单选按钮是互斥的,所以您必须将它们组合到 RadioGroup 中。 By grouping them together, the system ensures that only one radio button can be selected at a time.通过将它们组合在一起,系统确保一次只能选择一个单选按钮。
How to do this?这该怎么做?
RadioGroup is just like a view group for Radiobuttons so you can use it just like one RadioGroup 就像 Radiobuttons 的一个视图组,因此您可以像使用它一样使用它
XML: XML:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
C#: C#:
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.Orientation = Orientation.Horizontal;//or Orientation.VERTICAL
RadioButton rb = new RadioButton(this);// create the radiobutton
rg.AddView(rb);// add to radio group
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.