gpt4 book ai didi

c# - 如何选中单选按钮以及如何仅控制 ListView 中选中的一个单选按钮

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:24 26 4
gpt4 key购买 nike

我试图将我的数据添加到 ListView 中。我正在使用适配器。在 ListView 中有单选按钮和一些字段。

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
}
}

}

我可以在 ListView 中显示我的记录。但是单选按钮可以选择多个。我不想选择多条记录。

最佳答案

您可以创建一个集合来存储单选按钮的检查状态,然后在 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();

}
}

关于c# - 如何选中单选按钮以及如何仅控制 ListView 中选中的一个单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54211937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com