How to stop execution of piece of code in android until data is retrieved from firebase? : I am trying to perform some task based on the data retrieved from firebase.

        for (inti=0;i<dateList.size();i++)
        {
        attendanceDateRef= attendanceRef.child(dateList.get(i));
        attendanceClassRef= attendanceDateRef.child(ViewAttendanceSelectClassActivity.selectedClass);
        attendanceClassRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshotdataSnapshot) {
        for (DataSnapshotdsp : dataSnapshot.getChildren())
                       {
        attendanceList.add(dsp.getValue(String.class));
        Log.e("attendanceListValues",dsp.getValue(String.class));
        }
                }
        
        @Override
        public void onCancelled(DatabaseErrordatabaseError) {
        
                }
            });
        }
    
    
    intindex=1;
    
    for (inti=1;i<=attendanceList.size();i++)
    {
    if(i%4==0)
        {
    fullDateRangeList.add(dateList.get(index));
    index++;
    }
    else
    {
    fullDateRangeList.add(dateList.get(index));
    }
    
    }
    
    Log.e("fullDateRangeList",String.valueOf(fullDateRangeList.size()));
    Log.e("attendanceList",String.valueOf(attendanceList.size()));



above code is written on the OnClick event of a button, when I click on the button following output is generated on logcat:
fullDateRangeList:0
attendanceList:0
attendanceListValues:Value1
attendanceListValues:Value2
attendanceListValues:Value3
attendanceListValues:Value4
.
.
.
attendanceListValues:ValueN

from the above output it looks like second loop is executing before data is retrieved from firebase and that is why value of fullDateRange and attendanceList is 0.

is there any way i can prevent second loop from executing until data is stored in attendanceList?
