I make an application on android for my school but I block on a problem of asynchrony :
databaseReference2.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(final DataSnapshot ds: dataSnapshot.getChildren()){
                Log.e("test","1");
                Menu = ds.getValue(menu.class);
                list2.add(Menu.getId().toString());
                databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        try{
                            Log.e("test","2");
                            if(dataSnapshot.child("menuAjoute").child(mail).exists()){
                                    if(dataSnapshot.child("menuAjoute").child(mail).child(Menu.getId()).getValue().equals(Menu.getId())) {
                                        list.add(Menu.getNom()); 
                                    }
                            }
                        }catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
                Log.e("test","3");
            }
For my code to work, step 2 must be before step 3. But I have not found a way to do it. What would be the simplest way? (I searched for the solution but none of it worked).
 
     
    