0
votes

I have a list view in which inside each cell I have two textview, I need to handle the click event for one of the textview element. But when I put the click event inside the GetView() of the adapter, it is called multiple times.

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = tableItems[position];
        ViewHolder holder;
        View view = convertView;

            if (view == null)
            {
                LayoutInflater layoutInflator = LayoutInflater.From(mContext);
                view = layoutInflator.Inflate(Resource.Layout.myListViewCell, null);
                holder = new ViewHolder();
                holder.tvEmpName = view.FindViewById<TextView>(Resource.Id.tv_EmpName);
                holder.tvEmpPhone = view.FindViewById<TextView>(Resource.Id.tv_EmpPhone);
                view.Tag = holder;
            }

            else
            {
                holder = (ViewHolder)view.Tag;
            }

            holder.tvEmpName.Text = item.FullName;
            holder.tvEmpPhone.Text = item.Phone;

            holder.tvEmpPhone.Click += (sender, e) => {
               // Click event to launch the Popup menu
               // This event is being called multiple times, as Get view() being called multiple times.
            };

            return view;
   }

I gone through this similar thread, but didn't find any solution.

1
I tested your code without using ViewHolder, just access two TextView using view.FindViewById<TextView>(Resource.Id.;, the click event will be fired only once when the TextView is clicked. - Grace Feng
Grace Feng : In that case it is returning the invalid text string on tap of that textview, as the textview hold different data for each list view cell - Himanshu Dwivedi
Really? I customized a adapter which inherit from BaseAdapter<string>, it works fine. It returns exactly the item I clicked. I can't imagine what happens here, do you need my demo for testing? Or maybe you can share your demo to me? - Grace Feng
In case of my adapter which inherit from BaseAdapter<Model>, and it is returning sometimes valid and sometimes invalid as I scroll the list perform any kind of search in that case my list refreshes...and on tap it returns invalid string of the textview which is pone number in my case. - Himanshu Dwivedi
I am trying to implement IOnClickListener interface, once I get it woking fine, I will update my answer, still, if you get any resource plz update.. - Himanshu Dwivedi

1 Answers

0
votes

The event is called multiple times because evertime when the metod getview is called you add a click event with +=.

In this case you can put the click event inside the if, like that:

if (view == null)
{
      LayoutInflater layoutInflator = LayoutInflater.From(mContext);
      view = layoutInflator.Inflate(Resource.Layout.myListViewCell, null);
      holder = new ViewHolder();
      holder.tvEmpName = view.FindViewById<TextView>(Resource.Id.tv_EmpName);
      holder.tvEmpPhone = view.FindViewById<TextView>(Resource.Id.tv_EmpPhone);
      view.Tag = holder;
      holder.tvEmpPhone.Click += (sender, e) => {
           // Code here
      };
}

But the recyclerview is better than listview for this.