Reputation: 11595
Why doesn't my ItemClick event trigger?
_specialtyListView.ItemClick += (s, e) => _viewModel.Specialty = (string)_specialtyListView.Adapter.GetItem((int)e.Id);
NOTE:
- The listview is populated.
- Items receive focus.
However, I just cannot get the itemclick event to trigger.
I have the following initialization code for my ListView:
_specialtyListView = FindViewById<ListView>(Resource.Id.SpecialtyListView);
_specialtyListView.ChoiceMode = ChoiceMode.Single;
_viewModel.LoadSpecialties();
_specialtyListView.Adapter = new SpecialtiesAdapter(this, new List<string>(_viewModel.Specialties));
_specialtyListView.ItemClick += (s, e) => _viewModel.Specialty = (string)_specialtyListView.Adapter.GetItem((int)e.Id);
I then have the following ListViewItem layout:
<ListView
android:id="@+id/SpecialtyListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:paddingLeft="2dp" />
Here's the Layout for my ListItem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/ProviderSpecialty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
Upvotes: 0
Views: 56
Reputation: 11595
There's a bug where the breakpoint will NOT be triggered when the code is NOT wrapped in between braces:
listview.ItemClick += (s, e) => selectedItem = listview.GetItemAtPosition(e.Position).ToString();
To observe the breakpoint, I had to format the code as the following:
listview.ItemClick += (s, e) =>
{
selectedItem = listview.GetItemAtPosition(e.Position).ToString();
};
Upvotes: 0
Reputation: 24460
I suggest you switch to RecyclerView as it is has better memory handling; you can make updates to single items instead of being forced to update them all.
Anyways, I can't reproduce your issue locally. I did the following:
var listView = FindViewById<ListView>(Resource.Id.listview);
listView.ChoiceMode = ChoiceMode.Single;
listView.ItemClick += (s, e) =>
{
System.Diagnostics.Debug.WriteLine($"Clicked {e.Position}");
};
var items = new string[] { "Havarti", "Brie", "Cheddar", "Muenster", "Swiss" };
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
listView.Adapter = adapter;
I used the exact same layout as you posted. The ItemClick
handler is hit consistently.
Upvotes: 1