Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms - Get position of item selected in Listview

Im having a bit of trouble trying to get the position of an item in a ListView when its tapped.

Heres my code:

public MainPage()
{
    InitializeComponent();

    Results();
}

async void Results()
{
    var service = new RestService();
    response = await service.GetEventItemsAsync();

    Debug.WriteLine("Found: " + response.Count + " events");

    listView.ItemsSource = response;

    listView.ItemTapped += async (sender, e) =>
    {
        // Get position of item tapped 
    };
}

Any help would be greatly appreciated.

SOLVED

Had to change ItemTapped to ItemSelected

Then inside the ItemSelected I just added in this line:

var i = (listView.ItemsSource as List<EventItems>).IndexOf(e.SelectedItem as EventItems);
like image 541
ADuggan Avatar asked Apr 13 '16 09:04

ADuggan


1 Answers

In SelectedItem event try this:

var index = (myListView.ItemsSource as List<MyObject>).IndexOf (e.SelectedItem as MyObject);
like image 92
Shalva Avanashvili Avatar answered Nov 07 '22 22:11

Shalva Avanashvili