2

enter image description here

My current code is this:

String key = FirebaseDatabase.getInstance().getReference().child("posts").child("???").child("numLikes").toString;

How to get numLikes value?

2

1 Answer 1

2
final ArrayList<Integer> arrayList = new ArrayList<>();
        DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("posts");
        dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                arrayList.clear();
                for (DataSnapshot ds: dataSnapshot.getChildren()) {
                    int numLike = ds.child("numLikes").getValue(Integer.class);
                    arrayList.add(numLike);
                }
            }

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

            }
        });

Try this code, here you don't know pushkey so I have used forEach loop to jump one level down. The arraylist used here will contain all numLikes for your users.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.