2

I would like to know how to retrieve the data Please help! Thank you.

Here is my db structure in filebase.

db structure

 mDatabase = FirebaseDatabase.getInstance().getReference().child("chinese");
    mDatabase.keepSynced(true);

    mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
                Log.v(TAG,""+ childDataSnapshot.getKey()); //displays the key for the node
                Log.v(TAG,""+ childDataSnapshot.child("ingredient").getValue());   //gives the value for given keyname
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
1
  • I would like to know how to get the value(String) of each of the ingredient. Thanks Commented Mar 30, 2018 at 11:15

2 Answers 2

1
mDatabase = FirebaseDatabase.getInstance().getReference().child("chinese");
mDatabase.keepSynced(true);

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
                if (childDataSnapshot.child("ingredient").getValue() != null) {
                    ArrayList<String> ingredients = new ArrayList<>();
                    for (DataSnapshot ing : childDataSnapshot.child("ingredient").getChildren()) {
                        ingredients.add(ing.child("ingredient").getValue(String.class));
                    }
                    System.out.println("Gained data: " + ingredients.toString());
                }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Another way is casting DataSnapshot value to ArrayList:

GenericTypeIndicator<ArrayList<String>> t = new GenericTypeIndicator<ArrayList<String>>() {};
ArrayList<String> ingredients = childDataSnapshot.child("ingredient").getValue(t);
Sign up to request clarification or add additional context in comments.

Comments

1

you used below code to read firebase database.

 private void initView() {
    mFirebaseInstance = FirebaseDatabase.getInstance();
    mDatabase = mFirebaseInstance.getReference("usersDb/UserTable"); // root node and childNode Name
    mRvData.setLayoutManager(new LinearLayoutManager(this));
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                Log.v(TAG,""+ childDataSnapshot.getKey()); //displays the key for the node
                Log.v(TAG,""+ childDataSnapshot.child("ingredient").getValue());   //gives the value for given keyname
            }
         }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

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.