我试图将数据添加到列表视图。 我正在使用适配器。列表视图中有单选按钮和一些字段。
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
}
}
}
我可以在列表视图中显示我的记录。但是单选按钮可以选择多个。我不想选择多个记录。
答案 0 :(得分:1)
您可以创建一个集合来存储单选按钮的检查状态,然后在将集合加载到getView
中时加载该集合。
像这样:
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();
}
}
答案 1 :(得分:0)
您可以选择多个单选按钮的原因是它们不属于同一单选按钮组。
来自Android Radiobutton官方文档的引言。
要创建每个单选按钮选项,请在布局中创建一个RadioButton。但是,由于单选按钮是互斥的,因此必须在RadioGroup中将它们分组在一起。通过将它们分组在一起,系统可确保一次只能选择一个单选按钮。
如何执行此操作?
RadioGroup就像单选按钮的视图组一样,因此您可以像使用它一样使用它
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#:
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