shachar paz
shachar paz

Reputation: 1

xamarin item click in listview is not called

I used listView and when I try to triger the itemclick function it doesn't triger. I tried to figure that for the last 3 hours, pls help

in class:

li = (ListView)FindViewById(Resource.Id.listView1);
li.ItemClick += Li_ItemClick1;
li.ItemLongClick += Li_ItemLongClick;
li.Adapter = adapter;

in xml:

<ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1"
        android:layout_marginTop="10dp"
        />

Upvotes: 0

Views: 202

Answers (1)

Alkid Baci
Alkid Baci

Reputation: 43

You must set your Click event on OnBindViewHolder method of the listview adapter. Try this code :

((MyView)holder).mMainView.Click -= Li_ItemClick1;
((MyView)holder).mMainView.Click += Li_ItemClick1;

Also this is a good way to unsubscribe and subscribe the method to avoid setting multiple times. "MyView" is the class for the handler, to more specific, a handler is a class built with properties of your list item.

here is an example for MyView class:

 public class MyView : RecyclerView.ViewHolder
    {
       
        public View mMainView { get; set; }
        public TextView mName { get; set; }
        public TextView data { get; set; }
        public ImageView image { get; set; }
        public MyView(View view) : base(view)
        {
            mMainView = view;
            mName = view.FindViewById<TextView>(Resource.Id.text1);
            image = view.FindViewById<ImageView>(Resource.Id.foto);
            data = view.FindViewById<TextView>(Resource.Id.data);
        }
        public override string ToString()
        {
            return base.ToString() + " '" + mName.Text;
        }
    }

Here is a link that may be helpful: https://riptutorial.com/xamarin-android/example/30066/recyclerview-with-click-events

Note: RecycleView is a kind of ListView( i suggest using RecycleView because is simply ListView but improved and it makes it easier to do what you intend to). But if you want to use ListView this link is still helpful.

Upvotes: 0

Related Questions