0

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; }
        }
    }
}
0

1 Answer 1

2

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(){

};
Sign up to request clarification or add additional context in comments.

9 Comments

that works fine thank you. but is there a way to call this event from the activity? In eclipse i used to set to the button an onclicklistener and implement this listener from the activity. can i do the same here?
you can have button click in your activity by creating a method, public void HandleButtonClick (object sender, EventArgs e) { } in your activity and set this as the button click event inside your adapter. holder.Image.Click += yourActivityInstance.HandleButtonClick;
@Ironman this void btnoneclick is written in the activity right?
@chahidkhalil yes see the comment of HeisenBerg.
one more thing.. the returned position in the click event is not updated correctly . it gives me wrong position any idea about this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.