简体   繁体   中英

Android Studio is not reading the retreived data from Firebase realtime database

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

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;
 }

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");
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM