skip to Main Content

I am trying to retrieve a nested list of workouts from a Realtime Database and I don’t know how to do it.

I made some research and still couldn’t really figure out how am supposed to do it.

The Realtime Database JSON file looks like this :

screenshot

I am looking to retrieve data by workout, for example, if someone presses the workout one button I should retrieve the full workout one object. but I don’t know how am supposed to design my query request nor how am supposed to structure my model object that conceives the received data.

2

Answers


  1. I think below line code help you.

       databaseReference = FirebaseDatabase.getInstance().getReference().child("Fat Loss").child("Workout one").child("day 1")
    
    
    
    
         databaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        allTaskList.clear();
                        if (snapshot.exists()) {
                            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                               //modal class object
                               AddTODOListModal model = dataSnapshot.getValue(AddTODOListModal.class);
                                assert model != null;
                                model.setId(dataSnapshot.getKey());
                                allTaskList.add(model);
                            }
                            adapter = new TODOListAdapter(TODOListHomeActivity.this, allTaskList);
                            binding.rvTODO.setAdapter(adapter);
                        }else {
                            Utils.showToast("No Data Available");
                        }
                        Utils.dismissProgressDialog();
                    }
        
                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                        Utils.showToast(error.getDetails());
                    }
                });
    

    Reference link :- https://firebase.google.com/docs/database/android/lists-of-data

    Login or Signup to reply.
  2. As I see in your screenshot, under the "Workout one" node, you have two nested levels of data. So to get all exercises for each day, you have to loop over the children twice:

    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    DatabaseReference workoutOneRef = db.child("Fat Loss").child("Workout one");
    workoutOneRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DataSnapshot> task) {
            if (task.isSuccessful()) {
                for (DataSnapshot daySnapshot : task.getResult().getChildren()) {
                    for (DataSnapshot exerciseSnapshot : daySnapshot.getChildren()) {
                        String name = exerciseSnapshot.child("name").getValue(String.class);
                        Log.d("TAG", name);
                    }
                }
            } else {
                Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
            }
        }
    });
    

    Please also don’t forget that the Firebase API is asynchronous. So any code that needs data from the database needs to be inside the onComplete() method, or be called from there. To understand better, I recommend you check the following resource:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search