Skip to content Skip to sidebar Skip to footer

Firebaseui With Recycleview

After uptade syntax of FirebaseUI, can't work without onPopulateViewHolder method. I read the doc of FirebaseUI and did the same. After running app, RecycleView is running without

Solution 1:

To solve this, please follow the next steps:

  1. change your model to look like this:

    publicclassPlaces {
        privateString image, name;
    
        publicPlaces() { }
    
        publicPlaces(String image, String name) {
            this.image = image;
            this.name = name;
        }
    
        publicStringgetImage() { return image; }
        publicStringgetName() { return name; }
    }
    

    The fields from your model class should look exactly like the one from your database. In your code are different. See name_place vs. name.

  2. Make your firebaseRecyclerAdapter varaible global:

    privateFirebaseRecyclerAdapter<Places, PlaceViewHolder> firebaseRecyclerAdapter;
    
  3. Remove FirebaseRecyclerAdapter<Places, PlaceViewHolder> from the onCreate() method.

  4. Add the following lines of code in the onStart() and onStop() methods.

    @OverrideprotectedvoidonStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }
    
    @OverrideprotectedvoidonStop() {
        super.onStop();
        if(firebaseRecyclerAdapter != null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }
    

This is a complete example on how you can retrieve data from a Firebase Realtime database and display it in a RecyclerView using FirebaseRecyclerAdapter.

Edit:

To simply display those names in the logcat, please use the following code:

DatabaseReferencerootRef= FirebaseDatabase.getInstance().getReference();
DatabaseReferenceusersRef= rootRef.child("Users");
ValueEventListenervalueEventListener=newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Stringname= ds.child("name").getValue(String.class);
            Log.d("TAG", name);
            Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show());
        }
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(valueEventListener);

Post a Comment for "Firebaseui With Recycleview"