我如何将按钮单击计数器放入ListView适配器中

时间:2018-12-31 19:46:30

标签: listview android-listview xamarin.android onclicklistener

hi iam还是xamarin android的初学者:( 我通过单击按钮使listview生效,当我单击按钮时,我将计算点击次数并显示在button.text中,但我也无法处理点击情况

这是我的适配器GetView代码:

public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = items[position];
            View view = convertView;
            if (view == null) // no view to re-use, create new
                view = context.LayoutInflater.Inflate(Resource.Layout.list_ticket_view, null);
            view.FindViewById<TextView>(Resource.Id.maintxt).Text = item.maintitle;
            view.FindViewById<TextView>(Resource.Id.txtthwab).Text = item.thwab;

            var button1 = view.FindViewById<Button>(Resource.Id.button1);
             button1.Text = (item.counter).ToString();
         }

我让button1从item.counter开始取值并开始计数 即item.counter = 10来自课程 当单击将11,11,13,.. etc 最好的问候

1 个答案:

答案 0 :(得分:0)

您可以尝试:

class YourAdapter : BaseAdapter, View.IOnClickListener
    {

        private Dictionary<int, string> dictionary = new Dictionary<int, string>();

        int[] item;  //this is the data in my code ,you should replace your own data

    public YourAdapter(int[] value)  // the parameter use your own data
        {

            //copy your data into the dictionary
            item = value;
            for (int i = 0; i < item.Length; i++)
            {
                dictionary.Add(i,item[i].ToString());
            }
        }

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = items[position];
        View view = convertView;
        if (view == null) // no view to re-use, create new
            view = context.LayoutInflater.Inflate(Resource.Layout.list_ticket_view, null);
        view.FindViewById<TextView>(Resource.Id.maintxt).Text = item.maintitle;
        view.FindViewById<TextView>(Resource.Id.txtthwab).Text = item.thwab;

        var button1 = view.FindViewById<Button>(Resource.Id.button1);
        button1.Text = dictionary[position];
        button1.Tag = position;
        button1.SetOnClickListener(this);
        return convertView;
     }
}
 public void OnClick(View v)
        {
            var button = v as Button;
            int count = int.Parse(button.Text) + 1;
            button.Text = count.ToString();
            dictionary[(int) v.Tag] = count.ToString();
        }

让您的adapter工具View.IOnClickListener例如:

class MyAdapter : BaseAdapter ,View.IOnClickListener