2

I have one small code i don't know how to do it.

I have Realtime database in firebase, and i want check if child name is exist or not.

example:

Realtime database:

enter image description here

i tried before this code and the compiler is pass from this code and not execute it or going to check.

mDatabase.child ( "Voted" ).child ( DataTeamVote.name.get ( position ) ).addListenerForSingleValueEvent ( new ValueEventListener ( )
{
    @Override
    public void onDataChange ( DataSnapshot snapshot )
    {
        if ( snapshot.hasChild ( mAuth.getCurrentUser ( ).getUid ( ) ) )
        {
            check = false;
            Toast.makeText ( VoteAction.this , "You are already voted for this team" , Toast.LENGTH_SHORT ).show ( );
        }
        else
        {
            check = true;
            Toast.makeText ( VoteAction.this , "Open Vote" , Toast.LENGTH_SHORT ).show ( );
        }
    }

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

    }
} );
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Omar Naji
  • 21
  • 4
  • @AlexMamo: I reopened the question. Even though OP is trying to accomplish the same as in the answer you linked, they have code that should work. So linking the questions is great, but I wouldn't mark as a duplicate without further explanation as to why their current code doesn't work. – Frank van Puffelen Dec 18 '18 at 14:16
  • Hey Omar. When you run this code in the debugger and you place a breakpoint on the first line in `onDataChange`, does it get to that line? If so, what happens if you step through that method? – Frank van Puffelen Dec 18 '18 at 14:16
  • @FrankvanPuffelen Oh, that's right. wait to see Omar's answer. Thanks puf! – Alex Mamo Dec 18 '18 at 14:21
  • @FrankvanPuffelen no, it come to `mDatabase.child ( "Voted" ).child ( DataTeamVote.name.get ( position ) ) ` after that its go to another statement directly.. – Omar Naji Dec 18 '18 at 14:28
  • Did you put a **breakpoint** into the first line of `onDataChange`? There is no way to step into `onDataChange`. – Frank van Puffelen Dec 18 '18 at 15:19
  • Also: don't ignore errors. `public void onCancelled ( @NonNull DatabaseError databaseError ) { throw databaseError.toException(); }` – Frank van Puffelen Dec 18 '18 at 15:20

0 Answers0