============== Edito ============
Le agregue un contador para saber en que momento hacerle el callback.
public synchronized void next(String key,final RoomListQueryResultHandler handler) {
this.setLoading(true);
roomList = new ArrayList<Room>();
this.database.child("members").child(this.mUser.getUid()).child("rooms")
.orderByKey()
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
RoomListQuery.this.setLoading(false);
if (!dataSnapshot.hasChildren()) {
RoomListQuery.this.currentPage--;
}
// at the start we need to still load all children
final long[] pendingLoadCount = { dataSnapshot.getChildrenCount() };
for (DataSnapshot ds : dataSnapshot.getChildren()) {
//roomList.add(Room.upsert(room));
Room.getRoom(ds.getKey(), new Room.RoomGetHandler() {
@Override
public void onResult(Room room, customException e) {
if (e != null) {
// Error!
e.printStackTrace();
return;
}
roomList.add(room);
// we loaded a child, check if we're done
pendingLoadCount[0] = pendingLoadCount[0] - 1;
if (pendingLoadCount[0] == 0) {
if (handler != null) {
Friends.runOnUIThread(new Runnable() {
public void run() {
handler.onResult(roomList, (customException) null);
}
});
}
}
}
});
}
}
@Override
public void onCancelled(final DatabaseError databaseError) {
if (handler != null) {
Friends.runOnUIThread(new Runnable() {
public void run() {
handler.onResult((List) null, new customException(databaseError.toString()));
}
});
}
}
});
}