I'm trying to capture the item clicked on in a listview. Everything I've seen here deals with Xamarin Forms, I am NOT using Xamarin Forms. I build my listview and display it fine, but I cannot figure out how to capture the item click. My build code:
ListView PhoneNumberList;
PhoneNumberList = FindViewById<ListView>(Resource.Id.listViewNumbers);
ListOfPhoneNumbers = new List<string>();
if (PhoneNumberData.Tables[0].Rows.Count > 0)
{
foreach (DataRow MyDataRow in PhoneNumberData.Tables[0].Rows)
{
ListOfPhoneNumbers.Add(MyDataRow["FirstName"].ToString() " " MyDataRow["LastName"].ToString() " " MyDataRow["PhoneNumber"].ToString());
}
}
else
{
ListOfPhoneNumbers.Add("Currently there are no phone numbers available in this list
}
ArrayAdapter adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItemMultipleChoice, ListOfPhoneNumbers);
PhoneNumberList.Adapter = adapter;
This code displays the name and phone number as intended, but I cannot figure out how to detect when the Listview is clicked. I tried:
PhoneNumberList.ItemSelected = (sender, args) =>
{
var Sel = PhoneNumberList.SelectedItem;
};
But it never gets called on the click. I've also tried:
PhoneNumberList.ItemSelected = delegate (object sender, AdapterView.ItemSelectedEventArgs args) { PhoneNumberList_ItemSelected(); };
PhoneNumberList_ItemSelected never gets called either.
My Listview XML:
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="148.4dp"
android:id="@ id/listViewNumbers" />
So what am I doing wrong?
CodePudding user response:
Ok, after slogging around the internet for a few hours, I was able to work it out. It was rather simple and I had already discovered the answer, I just formated one statement wrong and it sent me on a wild goose chase to find the answer. My updated code:
ListView PhoneNumberList;
PhoneNumberList = FindViewById<ListView>(Resource.Id.listViewNumbers);
ListOfPhoneNumbers = new List<string>();
if (PhoneNumberData.Tables[0].Rows.Count > 0)
{
foreach (DataRow MyDataRow in PhoneNumberData.Tables[0].Rows)
{
ListOfPhoneNumbers.Add(MyDataRow["FirstName"].ToString() " " MyDataRow["LastName"].ToString() " " MyDataRow["PhoneNumber"].ToString());
}
}
else
{
ListOfPhoneNumbers.Add("Currently there are no phone numbers available in this list
}
ArrayAdapter adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItemMultipleChoice, ListOfPhoneNumbers);
PhoneNumberList.Adapter = adapter;
Exactly the same, now I add the one line of code I just couldn't get right (duh):
PhoneNumberList.ItemClick = PhoneNumberList_ItemClick;
Originally I had used PhoneNumberList.ItemClick = PhoneNumberList_ItemClick();
and the error it generated sent me off on that aforementioned wild goose chase and there went several hours of my life I'll never get back, lol. The PhoneNumberList_ItemClick
code:
void PhoneNumberList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
// We now can write code here to set the checkbox in the listview to checked
// All I got to do now is figure out how to do THAT. Stay tuned ....
}
So, hopefully this will save someone the ordeal I put myself through.
**** Update ****
Didn't take long to figure out how to set the item clicked's checkbok, changes to PhoneNumberList_ItemClick
code below:
void PhoneNumberList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
ListView MyListView = (ListView)sender;
if (MyListView.IsItemChecked(e.Position))
{
MyListView.SetItemChecked(e.Position, true);
}
else
{
MyListView.SetItemChecked(e.Position, false);
}
}
.... And that's all folks ....