0

enter image description here

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Absdqqqq
  • 43
  • 5
  • My first guess is that you downloaded the `google-services.json` file before you created the database in the console, and that means the database URL is missing or wrong in the file. If that is the cause, you should see a warning in your logcat output. See my answer here for the two possible solutions: https://stackoverflow.com/questions/68173632/google-firebase-real-time-database-not-working-as-everything-is-set-correctly/68179677#68179677 – Frank van Puffelen Dec 03 '22 at 19:25
  • No, that's not the issue, I checked and the url was printed correct in thee terminal – Absdqqqq Dec 04 '22 at 02:29
  • Is any of the onDataChange or onCancelled even triggered? Please respond using @AlexMamo – Alex Mamo Dec 04 '22 at 08:42

1 Answers1

0

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