Jarf
Jarf

Reputation: 61

Listview selecteditem in xamarin

I have a listview that is populated with a list of terms from ItemsSource. The .Name property is the text shown on the listview. I am trying to save the .Id int property to a variable so that I can access it from another form. Essentially I want it so that when a user clicks on an item on the list it will open a form and populate it with the instance of the list associated with the item tapped.

If the listview is populated with Book1, Book2, Book3, and Book4 with Ids of 1, 2, 3, and 4 then if the user taps Book2 I want the SelectedBook variable to be set to the .Id property of 2. Then when another form will be opened and this form will pull the appropriate book instance from the sqlite database and populate the fields with that book's information.

I have been trying to do so with the ItemSelected property of the ListView but have been getting this error "System.NullReferenceException: Object reference not set to an instance of an object.".

Here is the code that I used:

        public void OnSelectedItem(object sender, EventArgs args)
    {
        Book myBook = sender as Term;
        App.BookRep.SelectedBook = myBook.Id;
}

I originally had the listview populated with buttons (that is my preference) but the SelectedItem property of ListView didn't seem to be utilized when I did this. I tried using the Clicked property of Button but couldn't find a way to obtain the Id for the selected book in this way. Am I doing something incorrectly or is there a better way to go about doing this?

Upvotes: 0

Views: 2653

Answers (2)

Junior Jiang
Junior Jiang

Reputation: 12723

I originally had the listview populated with buttons (that is my preference) but the SelectedItem property of ListView didn't seem to be utilized when I did this.

There are two solution to get what you want:

Solution One: Should use SelectedItemChangedEventArgs to get SelectedItem

public void OnSelectedItem(object sender, SelectedItemChangedEventArgs args)
{
    Book myBook = (Book)args.SelectedItem;
    App.BookRep.SelectedBook = myBook.Id;
}

Solution Two:

public void OnSelectedItem(object sender, EventArgs args)
{
    Book myBook = ((ListView)sender).SelectedItem as Book;
    App.BookRep.SelectedBook = myBook.Id;
}

I tried using the Clicked property of Button but couldn't find a way to obtain the Id for the selected book in this way.

Solution Three:

If using Button click event to do , add Button in Xaml:

<Button Text="Click" HorizontalOptions="End" Clicked="OnClicked" />

OnClicked method is follow:

void OnClicked(Object sender, MyEventArgs args)
{
   Button btn = sender as Button;
   Book myBook = btn.BindingContext as Book;
   App.BookRep.SelectedBook = myBook.Id;
}

System.NullReferenceException: Object reference not set to an instance of an object.

If happen this error, check listview whether has some elements not used.Suggest that removing all controls from ListView, then check the select event can work.If not solved, you can show code of ListView.ItemTemplate,I will check that.

Upvotes: 2

Jason
Jason

Reputation: 89082

sender is the control that fired the event, not the data item

public void OnSelectedItem(object sender, EventArgs args)
{
    Book myBook = (Book)args.SelectedItem;
    App.BookRep.SelectedBook = myBook.Id;
}

Upvotes: 0

Related Questions