Reputation: 1693
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
Upvotes: 1
Views: 1764
Reputation: 1182
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.
Upvotes: 1