0

I'm relatively new to Android Studio, so I'm not very familiar with it yet. I have the following method in my main function:

 public void Regestrieren2( String AktuelleID){
int Aktuelleid = Integer.parseInt(AktuelleID);
        if (!BenutzernameRegestrieren.getText().toString().matches("") && !PasswortRegestrieren.getText().toString().matches("")){

            for (int i =1;i<Aktuelleid;i++){


                 reff = FirebaseDatabase.getInstance().getReference().child("UserDaten/User"+i);
                 reff.addValueEventListener(new ValueEventListener() {

                public void onDataChange(@NonNull DataSnapshot snapshot) {




                        String g = BenutzernameRegestrieren.getText().toString();
                        String myRefName= snapshot.child("Name").getValue().toString();
                        Log.d("Loool",myRefName);
                        Log.d("loool3",g);

                        if (myRefName.equals(g)){
                            Toast.makeText(Regestrieren.this, "Benutzername exestiert bereits", Toast.LENGTH_LONG).show();
                            setF(f=false);
                            Log.d("Loool4","drinnenNils");


                        }



                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });



            }


            Regestrieren3(AktuelleID,f);
            }




}

When I use the debugger, I notice that it always goes up to the "public void onDataChange" method in the For-loop and skips it. But I want it to go in there for every round. Could someone please help me? Thanks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • First step is to stop ignoring potential errors and implement `onCancelled`. At its minimum that should be public `void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }` – Frank van Puffelen Aug 25 '20 at 14:29
  • Next: you can't **step into** `onDataChange` since it is called asynchronously. If you want to see what happens inside `onDataChance`, put a breakpoint on the first line in there `String g = BenutzernameRegestrieren.getText().toString()` and let the debugger run until it hits that line (or the `onCancelled`). – Frank van Puffelen Aug 25 '20 at 14:30
  • @FrankvanPuffelen First of all, thank you very much for the quick answer. I added the DatabaseError Code. Strangely enough, it goes out of the For loop first and then the method is called later, which I don't understand. Then everything works correctly. Somehow it first gets out of the For-loop and after that it goes into the function? Do you have an idea how this can happen? – Nils Schmidt Aug 25 '20 at 15:06
  • `since it is called asynchronously.` – blackapps Aug 25 '20 at 15:28
  • As @blackapps commented, that is because data is loaded from Firebase asynchronously. Check out my answer here for a longer explanation and how to deal with it in your code: https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Aug 25 '20 at 16:11

0 Answers0