chahid khalil
chahid khalil

Reputation: 19

xamarin: android custom listview with button event

I am creating a custom list view in android (xamarin). I have the row design and the adapter and the activity. every thing run fine. Now in the row design I have an image button. Where and how to implement the click event of this Imagebutton if I need it to open a new activity. Note that the row click event works well and it is doing what it should do. I tried to implement the Imagebutton click in the adapter get view but the problem is that it is entering in this code multiple time witch is incorrect.

this is my adapter code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;

    namespace SocrateMobile.Droid.Adapter
    {
    class PulledItemList_Adapter : BaseAdapter<oneimg_twolbl>
    {
        private Activity context;
        private List<oneimg_twolbl> AllItemList;


        public PulledItemList_Adapter(Activity context, List<oneimg_twolbl>      AllItemList)
        {
            this.AllItemList = AllItemList;
            this.context = context;
        }
        public oneimg_twolbl GetItem_bypos(int position)
        {
            return AllItemList[position];
        }
        public override oneimg_twolbl this[int position]
        {
            get { return AllItemList[position]; }
        }

        public override int Count
        {
            get { return AllItemList.Count; }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            Holder_oneimg_twolbl holder = null;
            var view = convertView;

            if (view != null)
                holder = view.Tag as Holder_oneimg_twolbl;


            if (holder == null)
            {
                holder = new Holder_oneimg_twolbl();
                view = context.LayoutInflater.Inflate(Resource.Layout.oneimg_twolbl, null);
                holder.Text = view.FindViewById<TextView>(Resource.Id.text_list4_view);
                holder.Text2 = view.FindViewById<TextView>(Resource.Id.text_list4_view2);
                holder.Image = view.FindViewById<ImageButton>(Resource.Id.image_list4_view);
                view.Tag = holder;
            }

            var current_item = AllItemList[position];


            holder.Text.Text = current_item.FirstTxt;
            holder.Text2.Text = current_item.SecondTxt;
            holder.Image.SetImageResource(current_item.FirstImg);
            holder.Image.Click += (sender, e) =>
            {
               int x = position;
            };
            return view;
        }



        public class Holder_oneimg_twolbl : Java.Lang.Object
        {
            public TextView Text { get; set; }
            public TextView Text2 { get; set; }
            public ImageButton Image { get; set; }
        }
    }
}

Upvotes: 0

Views: 4128

Answers (1)

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20910

For the single event Occure you have use HasOnClickListeners.

Change this code

 holder.Image.Click += (sender, e) =>
    {
       int x = position;
    };

to this

 if(!holder.Image.HasOnClickListeners)
   {
      holder.Image.Click += (sender, e) =>
      {
         int x = position;
      };
   }

Update : To implement Method use below way.

holder.Image.Click += delegate{
        btnOneClick();
};

Create this way

void btnOneClick(){

};

Upvotes: 2

Related Questions