Reputation: 184
I want to make a query that checks whether a certain name (President or Secretary) exists inside a database. The structure of the database is as follows.
I have this code, but its not working. Is there something I'm doing 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) {
}
});
Upvotes: 3
Views: 47
Reputation: 598718
It seems you know the exact node you want to load, in which case you don't need an equalTo
. Instead you can look up the node with:
Query presidentquery = mDatabase.child("validate").child(uid).child("President");
The rest of your code can stay the same.
Upvotes: 3