0

How can I determine if the firebase recycler adapter returns null?

I have another action when the user searches for a book and return null.

FirebaseRecyclerAdapter<Books, BooksViewHolder> adapter = new FirebaseRecyclerAdapter<Books, BooksViewHolder>(
            Books.class,
            R.layout.book_page,
            BooksViewHolder.class,
            bookTitleRef.orderByChild("title").startAt(search_title).endAt(search_title + "\uf8ff")) {
        @Override
        protected void populateViewHolder(final BooksViewHolder booksViewHolder, final Books books, final int position) {
            final String book_id = getRef(position).getKey().toString();
            Log.i(TAG, "populateViewHolder: " + book_id);

        }
    };

Thanks...

Eleazer Toluan
  • 233
  • 4
  • 14
  • 1
    To handle the case where no data exists, you will need to attach a separate ValueEventListener. Have a look here: http://stackoverflow.com/q/40201574/. There is an open feature request on FirebaseUI to add this feature: https://github.com/firebase/FirebaseUI-Android/issues/118 – Frank van Puffelen Dec 06 '16 at 04:53
  • Thanks you very much, but how can I actually know if it is null? – Eleazer Toluan Dec 06 '16 at 09:08
  • I tried datasnapshot.exist() on valueEventListener but it is not working. – Eleazer Toluan Dec 06 '16 at 09:17
  • If you have a `ValueEventListener` on a query, `dataSnapshot.exists()` will always be true (since the location that you're running the query on exists). Instead you'll want to test `dataSnapshot.getChildrenCount() > 0` – Frank van Puffelen Dec 06 '16 at 09:43
  • It fixed my problem, thanks. – Eleazer Toluan Dec 07 '16 at 05:06

0 Answers0