Firebase query logic not working in android

Ngugi Kariuki :

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.

Firebase database

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) {
            }
        });
Frank van Puffelen :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=374693&siteId=1