无法从Android Studio中的Firebase实时数据库检索数据

wd2eg0qa  于 2022-12-04  发布在  Android
关注(0)|答案(1)|浏览(194)

我想访问img,但当我尝试没有工作

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

当我进入onDataChange函数时,即使11111111也没有打印出来
问题是什么?如何获取所需的数据?

6yt4nkrj

6yt4nkrj1#

试试这个

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

建立时

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

相关问题