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.


Solution 1:

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.

Solution 2:

With ref.child("[email protected]").child("_email") you are just setting the reference to the object. Take a look at the java firebase documentation to retrieve data.

To get the data you will need to set a listener for your reference

ref.child("[email protected]").child("_email").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        // data available in snapshot.value()
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
    }
});