如何获取选中的单选按钮以及如何仅控制列表视图中选择的一个单选按钮 [英] How to get checked radio button and how to control only one radio button selected in list view

查看:58
本文介绍了如何获取选中的单选按钮以及如何仅控制列表视图中选择的一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据添加到列表视图.我正在使用适配器.列表视图中有单选按钮和一些字段.

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.

推荐答案

您可以创建一个集合来存储单选按钮的检查状态,然后在将集合加载到 getView 中时加载该集合.像这样:

You could create a collection to store the check status of the radiobutton, and then load the collection when it is loaded in 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();

      }
   }

这篇关于如何获取选中的单选按钮以及如何仅控制列表视图中选择的一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆