retrive data from firebase of list of saved ids

Viewed 38

I have list of node's ids and i want to retrive all child of that node.

This is my data

enter image description here

where each id is a recipe id :

This my code to retrive the ids then i save it in an arrylist :

    data_ref_user = FirebaseDatabase.getInstance().getReference("Users").child(current_uid).child("My Recipes");
data_ref_user.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        if (snapshot.exists()){

            for (DataSnapshot dataSnapshot:snapshot.getChildren()){
                String recipe_id = dataSnapshot.getValue().toString();
                list_recipes_id.add(recipe_id);
            }//end for

        }else {
            Toast.makeText(getContext(), "No data found!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException();

    }
});

And then i try to loop throught list_recipe_id adding a valueEventListener and saving the recipe into recipe_list .

    data_ref_recipe = FirebaseDatabase.getInstance().getReference("Recipes");
data_ref_recipe.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {

        for(DataSnapshot data:snapshot.getChildren()) {

            for (String ids :list_recipes_id){
                Recipe recipe = data.child(ids).getValue(Recipe.class);
                recipe_list.add(0,recipe); 
            }//end inner loop

            Recipe recipe = data.getValue(Recipe.class);
            recipe_list.add(0,recipe); //0 to put data in first not buttom

        }
        newAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException();

    }
});

i hope i can find a way to retrive the data of all saved id?

0 Answers
Related