2

I am trying to read data from Firebase but it is not read by android studio although I am using the tutorial

I tried to copy the link that is sent by Android Studio to Firebase:

DatabaseReference myRef = database.getReference("USERS").child(uID).child("DeviceID"); textview.setText(myRef.toString());

and past the result in the browser and it shows me the result in firebase but when I try to use it to get data it is not retrieving anything. here is how I am trying to read it by calling a function:

textview.setText(ReadDeviceId);

''''''''''''''''''''''''''''''''''''

   private String ReadDeviceId(){

    FBUser = FirebaseAuth.getInstance().getCurrentUser();
    uID = FBUser.getUid();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("USERS").child(uID).child("DeviceID");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            r_deviceID = dataSnapshot.getValue(String.class);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            r_deviceID = "no userID";
        }
    });
    return r_deviceID;
}

''''''''''''''''''''''''''''''''''''''''''' Knowing that my firebase database security rule is: '''''''''''''''''''''''''''''''''

{

"rules": {

".write": "auth != null",

".read": true

}

}

'''''''''''''''''''

but nothing is displayed

rainer
  • 3,295
  • 5
  • 34
  • 50

2 Answers2

0

you can try this code

 private String ReadDeviceId(){
      FBUser = FirebaseAuth.getInstance().getCurrentUser();
      uID = FBUser.getUid();
      FirebaseDatabase database = FirebaseDatabase.getInstance();
      DatabaseReference myRef = 
           database.getReference("USERS").child(uID).child("DeviceID");

     myRef.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
             for(Datasnapshot snapshot : dataSnapshot.getChildren)
             {
                //try to map it with your on model class and replace the String with your model class
                r_deviceID = snapshot.getValue(String.class)
             }
             //r_deviceID = dataSnapshot.getValue(String.class);
          }

          @Override
          public void onCancelled(DatabaseError error) {
        // Failed to read value
             r_deviceID = "no userID";
           }
     });
     return r_deviceID;
 }
HussainAbbas
  • 242
  • 2
  • 13
0

In your ReadDeviceId() function, you are returning the value of r_deviceID before it is set (event listeners are not synchronous). Therefore, your textview.setText(ReadDeviceId()); will always be textview.setText(null);, which will show nothing.

For your use case, you should change addValueEventListener to addListenerForSingleValueEvent and set the textview's value from within the handler itself like this:

private String ReadDeviceId(){
    FBUser = FirebaseAuth.getInstance().getCurrentUser();
    uID = FBUser.getUid();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("USERS").child(uID).child("DeviceID");

    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                textview.setText(dataSnapshot.getValue(String.class));
        }

        @Override
        public void onCancelled(DatabaseError error) {
                // Failed to read value
                textview.setText("no userID");
        }
    });
}
samthecodingman
  • 23,122
  • 4
  • 30
  • 54
  • i tried to make the program hold 5 second before running the getDeviceId function and it return the value... but i don't know if it is recommended to make the program hold like this – user11328771 Apr 08 '19 at 14:36
  • That's a separate question entirely. Take a look at [this answer](https://stackoverflow.com/a/3039718/3068190) for how to properly delay function execution. – samthecodingman Apr 08 '19 at 22:49