Firebase query logic does not work in Android… here is a solution to the problem.
Firebase query logic does not work in Android
I want to make a query to check if a certain name (president or secretary) exists in the database.
The database structure is as follows.
I have this code but it doesn’t work. Am I doing something wrong?
mDatabase = FirebaseDatabase.getInstance().getReference();
Query presidentquery = mDatabase.child("validate").child(uid).equalTo("President");
presidentquery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Candidate p = dataSnapshot1.getValue(Candidate.class);
president.setEnabled(false);
president.setText("Voted Already");
}
}
else{
president.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Home.this, AllCandidates.class));
finish();
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Solution
You seem to know the exact node to load, in which case you don’t need equalTo
. Instead, you can find the node in the following way:
Query presidentquery = mDatabase.child("validate").child(uid).child("President");
The rest of your code can remain unchanged.