How to know which row tapped or selected in ListView Xamarin Form

12,469

I have a list view and on click I have to go to the details view page. The following is the code that I use for the same.

listView.ItemSelected += async (sender, e) =>
            {
                if (e.SelectedItem == null)
                {
                    // don't do anything if we just de-selected the row
                    return;
                }
                else
                {
                    Resource resource = e.SelectedItem as Resource;
                    listView.SelectedItem = null;
                    await Navigation.PushAsync(new ResourceDetails(resource));
                }
            };

In your case I would modify the code as follows :

listView.ItemSelected += async (sender, e) => {

    if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row

    await DisplayAlert("Tapped", (e.SelectedItem as YourDataType).Name + " row was selected", "OK");

    ((ListView)sender).SelectedItem = null; // de-select the row
};
Share:
12,469
Le Anh Tai
Author by

Le Anh Tai

Updated on June 04, 2022

Comments

  • Le Anh Tai
    Le Anh Tai about 2 years

    I am new to Xamarin. I have a question about ListView in Xamarin form. How do I know which row or index everytime I tab (or select) in the listView? Below is the code I tried, but e.SelectedItem does not show anything. Thanks for helping.

    listView.ItemSelected += async (sender, e) => {
    
        if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row
    
        await DisplayAlert("Tapped", e.SelectedItem + " row was selected", "OK");
    
        ((ListView)sender).SelectedItem = null; // de-select the row
    };