Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Listview.ItemClick not functioning

meirrotfleischmeirrotfleisch USMember ✭✭✭
edited May 25 in Xamarin.Android

HI

I have an activity with a ListView connected to a custom adapter

The listview layout consists of some textviews and a Switch.

When item is clicked its not calling the listItem_Click function. But If i remove the "Switch" from the Layout then the itemclick works correctly.

Is this a bug?

Looking forward to your kind help

Regards

M.R.

Best Answer

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    You can just add a click event for your convertView in your adapter , you can refer to the following code:

    public override View GetView(int position, View convertView, ViewGroup parent)
            {
                var t = listStudents[position];
                View v = convertView;
    
                v = a.LayoutInflater.Inflate(Resource.Layout.activity_image, null);
                Button Fermer = v.FindViewById<Button>(Resource.Id.button1);
                TextView name_tv = v.FindViewById<TextView>(Resource.Id.nametxt);
    
               Switch mSwitch = v.FindViewById<Switch>(Resource.Id.mSwitch);
    
                name_tv.Text = t.Nom;
    
                v.FindViewById<ImageView>(Resource.Id.imageView1).SetImageResource(t.Image);
    
                v.Click += delegate {
                    Toast.MakeText(a, "Test..", ToastLength.Long).Show();
                };
    
                return v;
            }
    

    The result is:

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    You can just add a click event for your convertView in your adapter , you can refer to the following code:

    public override View GetView(int position, View convertView, ViewGroup parent)
            {
                var t = listStudents[position];
                View v = convertView;
    
                v = a.LayoutInflater.Inflate(Resource.Layout.activity_image, null);
                Button Fermer = v.FindViewById<Button>(Resource.Id.button1);
                TextView name_tv = v.FindViewById<TextView>(Resource.Id.nametxt);
    
               Switch mSwitch = v.FindViewById<Switch>(Resource.Id.mSwitch);
    
                name_tv.Text = t.Nom;
    
                v.FindViewById<ImageView>(Resource.Id.imageView1).SetImageResource(t.Image);
    
                v.Click += delegate {
                    Toast.MakeText(a, "Test..", ToastLength.Long).Show();
                };
    
                return v;
            }
    

    The result is:

  • meirrotfleischmeirrotfleisch USMember ✭✭✭

    @jezh... as always a great helper :)

    Thanks again

  • jezhjezh Member, Xamarin Team Xamurai

    It's my pleasure to help you. Happy coding... :)

Sign In or Register to comment.