Skip to content Skip to sidebar Skip to footer

Check Data If Exist In Firebase

Good Day, I was thinking about authenticating a user if he/she is voted based on their student_id. I tried retriving the data to the android studio but it always falls on the else.

Solution 1:

Since you're already querying for Pstud_no, you don't have to check for a child node with that key in the onDataChange. Instead you can just check if the query returned a data snapshot that exists:

Query VerifySSCPresident = userReference.orderByChild("student_no").equalTo(Pstud_no);
VerifySSCPresident.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            Toast.makeText(Voter_Screen.this, "Already Voted", Toast.LENGTH_SHORT).show();
        }
        else {
            Intent intent = new Intent(Voter_Screen.this, Voter_SSC_President.class);
            startActivity(intent);
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }

Post a Comment for "Check Data If Exist In Firebase"