0
votes

I have a listview where I retrieve data from firebase with an helper and show it by binding them in .xaml, like that:

the helper:

public class FirebaseHelper
public async Task<List<Books>> GetAllBooks()
        {
            return (await firebase
              .Child("Books")
              .OnceAsync<Books>()).Select(item => new Books
              {
                  Title = item.Object.Title,
                  Author = item.Object.Author,
                  Genre = item.Object.Genre,
                  Cover  = item.Object.Cover
              }).ToList();
        }

the page.xaml.cs

public List<Books> libriall;

protected async override void OnAppearing()
        { 
            base.OnAppearing();
            bookall = await firebaseHelper.GetAllBooks();
            listbook.ItemsSource = bookall;
        }

and a part of the listview listbook in the .xaml file:

<Label 
       TextColor="#33272a"
       Text="{Binding Title}"
       x:Name="bTitle"/>

Ok now I put a Button in the ViewCell and I want to get the book title and use it in a PostAsync, so I basically need to get the single title book and put it in a string.

Already created a method like that in the helper:


public async Task<string> getTitle()
        {
            return (await firebase
              .Child("Books")
              .OnceAsync<Books>()).Select(item => new Books
              {
                  Title = item.Title
              }).ToString();
        }


But I don't know how to link books properties to the single viewcell they are displayed with the binding, any idea?

1
if GetAllBooks retrieves the title, why do you need another method getTitle to also get the title?Jason
getAllBooks returns a List, GetTitle returns a string insteadFio
Yes, but the list contains a list of Book, and each Book already has a Title property. Why do you need to retrieve it again, instead of just pulling it from the Book that you already have? You are clicking a row in your list, you just want to get the title of the book for that row?Jason
I only want the title from the row I'm interacting with, if I use GetAllBooks I get the list of all the books with their properties while I only want a string.Fio
you do not need to call GetAllBooks again, you already have all of that data in the ItemsSourceJason

1 Answers

1
votes

you do not need to get the data from your service again, you already have it in the ItemsSource for your list

void ButtonClicked(object sender, EventArgs args)
{
  Button btn = (Button)sender;

  // the BindingContext of the Button will be the Books
  // object for the row you clicked on 
  var book = (Books)btn.BindingContext;

  // now you can access all of the properties of Books
  var title = book.Title;
}