Skip to content Skip to sidebar Skip to footer

Why Is The Childeventlistener Not Working?

The code for using the childEventListener looks like this: DatabaseReference TUidRef = usersRef.child(td); Log.i('hello',td); TUidRef.addChildEventListener

Solution 1:

In that case you should use a ValueEventListener, which listens to a single node at once. The code is quite similar:

DatabaseReference TUidRef = usersRef.child(td);

TUidRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        ms = dataSnapshot.child("rec_msg").getValue(String.class);
        fUid = dataSnapshot.child("fromUID").getValue(String.class);
        st = dataSnapshot.child("rec_secret").getValue(String.class);

        int km = 0;
        try {
            km = Integer.parseInt(st);
        }
        catch (NumberFormatException e) {
            Log.d("NumberError:", "Can't convert");
        }

        if(ms!=null && !ms.equals(""))
            addMessageBox(ms,km);
        else
            Toast.makeText(Main5Activity.this,"Something went wrong",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

Post a Comment for "Why Is The Childeventlistener Not Working?"