Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving child value -firebase-

System.out.println(ref.child("[email protected]").child("_email"));

*i`m trying to get a value of child but all time i get the URL of the value how to get the value of this URL as i try by this code but it get me the URLi want to get the _email value.

like image 684
Mohamed Farahat Avatar asked Jun 24 '16 16:06

Mohamed Farahat


1 Answers

You are looking at the concept from the wrong angle. While using the ref.child("[email protected]").child("_email") you are just simply pointing at a particular place in your database and nothing more. If you want to retrieve the data in that particular place, consider these 2 ways.

First if you want to retrieve the data only once, you can do the following :

 DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
 DatabaseReference mostafa = ref.child("Users").child("[email protected]").child("_email");

 mostafa.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
    String email = dataSnapshot.getValue(String.class);
    //do what you want with the email 
 }

 @Override
 public void onCancelled(DatabaseError databaseError) {

 }
 });

or maybe you want to retrieve the value in real time and use it in the same time that the database value is changed, all in the same time, whenever the value in changed, then you use this :

mostafa.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    String email = dataSnapshot.getValue(String.class);

    display.setText(email);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

Note the difference between the two methods. First is only for one time retrieve and the second is for retrieving the data whenever the value is changed.

Just have in mind that the codes that i posted are just templates and may need to play with them a bit.

like image 145
Ramtin Avatar answered Sep 24 '22 05:09

Ramtin