Reputation: 13
I am developing an app where the user registers, enters some information and is then redirected to another page where they can see the information they entered. The information is saved in a real time database using firebase and my problem is how to retrieve that data to display? I have looked at tutorials but i do not understand how this data is able to be retrieved in an array list or something like that.
Here is my database structure, where the id is the user's uuid:
And here is how i save the data to the database:
this is the class constructor that holds the different variables being saved
public PersonalInfo(String Id, String Name, String Birthday, String weight, String height, String gender, String Protein, String carbs, String fat, String Weightloss)
{
this.id = Id;
this.Name = Name;
this.Birthday = Birthday;
this.weight = weight;
this.height = height;
this.gender = gender;
this.Protein = Protein;
this.carbs = carbs;
this.fat = fat;
this.weightloss = Weightloss;
}
and after getting the text of the different text fields in the activity the data is saved like this:
PersonalInfo info = new PersonalInfo(Id, Name, Birthday, Weight, Height, Gender, Protein, Carbs, Fat, Weightloss);
databaseinfo.child(Id).setValue(info);
Toast.makeText(this, "Data added", Toast.LENGTH_SHORT).show();
If anyone could help or give me a link to a tutorial that explains my problem, it would be much appriciated.
Upvotes: 1
Views: 1269
Reputation: 1795
Hi just add this all into a method and call it & replace 'server/saving-data/fireblog/posts'
with your server node link and you will get the reponse from that node.
// Get a reference to our posts
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog/posts");
// Attach a listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Post post = dataSnapshot.getValue(PersonalInfo.class);
System.out.println(post);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
There are several types of event listeners are present & you can read all about retrieval of data from here.
Upvotes: 1