简体   繁体   中英

Can't retrieve data from Firebase Realtime db in Android Studio

在此处输入图像描述

I want to access the img but when I try nothing works

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("med");

System.out.println(myRef);
System.out.println(database.getReference().child("med"));

database.getReference().child("med").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println("11111111");
    }

    @Override
    public void onCancelled(DatabaseError error) {
        // Failed to read value
        Log.w(TAG, "Failed to read value.", error.toException());
    }
});

Even 11111111 is not printed when I enter the onDataChange function

What's the problem and how do I get the data I need?

Try this

//Getter and Setter Class
   public class GetSet {
        public String img;
    
        public String getImg() {
            return img;
        }
    
        public void setImg(String img) {
            this.img = img;
        }
    }

OnCreate

final FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference ref = database.getReference().child("med").child("1");
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                GetSet adapter = dataSnapshot.getValue(GetSet.class);
                String image=adapter.getImg();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

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