简体   繁体   中英

How can i bring just one data from firebase database?

I want to know how to get one data from the list of key

i know the key

but what i want to just get from one data what i saved

i don't understand why it is null

in the value 'petcode' is saved key

在此处输入图片说明

This is what i trying to get the data petname

reference = FirebaseDatabase.getInstance().getReference().child("Pets").child(firebaseUser.getUid()).child(petcode);


        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                petname = String.valueOf(dataSnapshot.child("petname"));
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

but when i see the log petname value is null how can i get data from it?

try this:-

ref.child("-LiCZpsymgNgtNcCinpHR5").child("petname").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        try {
            if (snapshot.getValue() != null) {
                try {
                    Log.e("TAG", "" + snapshot.getValue()); // your name values you will get here
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("TAG", " it's null.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        Log.e("onCancelled", " cancelled");
    }
});
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Pets").child(firebaseUser.getUid()).child(petcode).child("petname");
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String petname = dataSnapshot.getValue(String.class);                       
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                conformDialog.setMessage("Something bad happened!, try again");
                conformDialog.setCancelable(true);
                conformDialog.setCanceledOnTouchOutside(true);
            }
        });

and do not use addValueEventListener for a single event, it will monitor forever

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