I have successfully been able to save data in RTD of Firebase using
databaseIngredients.child(StringId).setValue(ingredients);
Where the "ingredients" is a String taken from EditText and StringId is a unique ID by the "push" method. I have referred to many tutorials but I am not able to read it, I want to update my RecyclerView every time my database is updated with new data. I just want to display all data in my Database using recyclerView. The ValueEventListener method is not understood by me. Please be patient as I understand the reply, new to Firebase.
Asked
Active
Viewed 1,221 times
0
-
**[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Jun 06 '18 at 08:08
-
Thank you, but the issue has been resolved – Jun 10 '18 at 08:26
2 Answers
0
Reading data is very simple, all you need to do is set a listener
that listens for change in value
in the database reference
you provided.
There are two listeners for that purpose,
- One Time Listener(
addListenerForSingleValueEvent
), which only reads data once you make request. For this,
DatabaseReference reference = database.getReference("Database").child(StringId).setValue(ingredients);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapShot.exists()){
//update recyclerview or do anything with your data
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Continuous Data Listener(
addValueEventListener
), which is used to get data every time it changes in serverDatabaseReference reference = database.getReference("Database").child(StringId).setValue(ingredients);
reference.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapShot.exists()){ //update recyclerview or do anything with your data } } @Override public void onCancelled(DatabaseError databaseError) { Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show(); } });

SafalFrom2050
- 687
- 6
- 12
-
Thanks for the detailed help, but how will I display my data In the RecyclerView? – Jun 05 '18 at 17:28
-
That is very simple too. There are many ways you can do it. This simple example will help you. :) https://www.android-examples.com/show-firebase-database-data-into-recyclerview/ – SafalFrom2050 Jun 12 '18 at 10:26
0
You can use FirebaseRecyclerAdapter . You'll need a model class and a ViewHolder class.
If you're using new version of firebase-ui
then you have to use FirebaseRecyclerOptions along with FirebaseRecyclerAdapter

parag pawar
- 193
- 3
- 13