1

i use a mono andriod ListView ,my listview contains 2 textview and one Image

and my following code works

  listView.ItemClick += (sender, e) =>
    {
        //Get our item from the list adapter
        var item = this.listAdapter.GetItemAtPosition(e.Position);

        //Make a toast with the item name just to show it was clicked
        Toast.MakeText(this, item.Name + " Clicked!", ToastLength.Short).Show();
    };

But when i put a button inside Listview,then this event not works and i am not able to work on button click.so how i handle Button click inside Listview in Mono andriod

Vishwajeet
  • 1,575
  • 2
  • 19
  • 35
  • Seems like [this](http://stackoverflow.com/questions/3045872/listview-and-buttons-inside-listview) would be the answer to your question. – Bryan Aug 21 '12 at 13:40

1 Answers1

1

If you want the click handler specifically on the button inside the listview you need something like this:

public class CustomListAdapter: BaseAdapter {
    public CustomListAdapter(Context context, EventHandler buttonClickHandler) {
        _context = context;
        _buttonClickHandler = buttonClickHandler;
    }

    public View GetView(int position, View convertView, View parent) {
        var itemView = convertView;

        if(itemView == null) {
            var layoutInflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
            itemView = layoutInflater(Resource.Layout.ItemView);
        }

        var button = itemView.FindViewById<Button>(Resource.Id.MyButton);
        button.Click += _buttonClickHandler;
    }

    // ... Rest of the code omitted for simplicity.
}

This code does not take into account the fact that there could be another handler attached to the button. Make sure that you decouple the old one, before coupling a new one. Or add some sort of detection that you've added the click handler before and don't add another one.

Willem Meints
  • 1,152
  • 14
  • 33
  • how we pass buttonClickHandler at the time of binding the list view adapter with CustomListAdapter – Vishwajeet Sep 26 '12 at 11:43
  • Assuming you're using a list activity, you would pass it in like this: this.Adapter = new CustomListAdapter(this,this.OnButtonClick); you basically point it to the method name. – Willem Meints Sep 28 '12 at 09:18