Every time I run this code the for
loop stops after some random number of iterations, but not 100. If I comment out the data retrieving part from firebase, the for
loop runs fully.Can I know the reason why the loop stops in between?
firstslot = new ArrayList<>();
for (i =1; i<=100; i++)
{
final String string = String.valueOf(i);
reference.child(string)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if ((dataSnapshot.exists()) && (dataSnapshot.hasChild("name")) && (dataSnapshot.hasChild("age"))) {
String check = "BOOKED";
item = new book(string, deptid, date, check, Color.CYAN);
firstslot.add(item);
} else {
String check = "";
item = new book(string, deptid, date, check, Color.TRANSPARENT);
firstslot.add(item);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
RecyclerView myrv = findViewById(R.id.booksrecyclerview);
bookadapter myadapter = new bookadapter(this, firstslot);
myrv.setLayoutManager(new GridLayoutManager(this, 5));
myrv.setAdapter(myadapter);
}