Usually I just set the ItemClick event on the ListView itself and not
on each of the items created by the adapter.
So instead of having your ItemClickHandler method in the adapter and
setting that for each item do something like this:
* In your activity where you set up the ListView and ListAdapter which
could look like this:
var listView = FindViewById<ListView>(Resource.Id.ShellItemsList);
listView.Adapter = new ShellListAdapter(this);
* Then just add an event handler for the ItemClick event on the listView:
listView.ItemClick += ListViewOnItemClick;
private void ListViewOnItemClick(object sender,
AdapterView.ItemClickEventArgs itemClickEventArgs)
{
//Get our item from the list adapter
var item =
listAdapter.GetItemByShellId(itemClickEventArgs.Position);
//do whatever here with the item...
}
On Tue, Sep 4, 2012 at 10:06 AM, Александр <[email protected]> wrote:
> Hello.
>
> I've made a ListView with custom Adapter and ListItem, but event "ItemClick"
> is not working.
>
> internal class ShellListAdapter:BaseAdapter<Shell>
> {
> private Activity _context;
> private List<Shell> _items;
> private OnShellItemClick _handler;
>
> /// <summary>
> /// Initializes a new instance of the <see
> cref="ClientWorkerAndroid.ShellListAdapter"/> class.
> /// </summary>
> ///
> /// Context. Текущая activity
> ///
> ///
> /// Items. Список объектов
> ///
> ///
> /// Listener. Обработчик клика на элемент
> ///
> public ShellListAdapter (Activity context, List<Shell> items,
> OnShellItemClick listener): base()
> {
> _items = items;
> _context = context;
> _handler = listener;
> }
>
> public override long GetItemId (int position)
> {
> return position;
> }
>
> public override Shell this [int position] {
> get { return _items [position]; }
> }
>
> public override Java.Lang.Object GetItem (int position)
> {
> return position;
> }
>
> public override int Count {
> get { return _items.Count; }
> }
>
> public override View GetView (int position, View convertView,
> ViewGroup
> parent)
> {
> var item = _items [position];
> View view = convertView;
> if (view == null)
> view = _context.LayoutInflater.Inflate
> (Resource.Layout.ListShellsItem,
> null);
> view.FindViewById<TextView> (Resource.Id.ShellId).Text =
> item.id;
> view.FindViewById<ImageView>
> (Resource.Id.ShellIcon).SetImageResource
> (item.icon);
> //Ужасный костыль :(
> view.Click -= ItemClickHandler;
> view.Click += ItemClickHandler;
>
> return view;
> }
>
> public Shell GetItemAtPosition (int position)
> {
> return _items [position];
> }
>
> public Shell GetItemByShellId (string shellId)
> {
> var sItem = _items.Find (value => value.id == shellId);
>
> if(sItem==null)
> return null;
>
> return sItem;
> }
>
> private void ItemClickHandler (object sender, EventArgs e)
> {
> var view = sender as LinearLayout;
> _handler (view.FindViewById<TextView>
> (Resource.Id.ShellId).Text);
> }
> }
>
>
>
> --
> View this message in context:
> http://mono-for-android.1047100.n5.nabble.com/ListViewItem-selection-tp5711677.html
> Sent from the Mono for Android mailing list archive at Nabble.com.
> _______________________________________________
> Monodroid mailing list
> [email protected]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
--
Med Venlig Hilsen / With Best Regards
Tomasz Cielecki
http://ostebaronen.dk
_______________________________________________
Monodroid mailing list
[email protected]
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid