1

i have a database node called Stations and inside there will be various items associated with that station. eg the name/title description , some of these children will contain a Boolean called PROMOTED if this Boolean equals true i want it to appear in my promoted tab with in my app. how can i make sure only these children appear in there? at the moment my fragment is just listing all stations. example below

would it be something to do with query?

ref = db.getReference().child(DATABASE_CHILD);

    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot snap : snapshot.getChildren()) {
                String Title = snap.child("TITLE").getValue(String.class);
                String Desc = snap.child("DESCRIPTION").getValue(String.class);
                String  Thumbup = snap.child("THUMBUP").getValue(String.class);
                String Thumbdown = snap.child("THUMBDOWN").getValue(String.class);
                String Image = snap.child("IMAGE").getValue(String.class);
                setTextViews(Title,Desc,Thumbup,Thumbdown,Image);
            }
        }
3
  • 1
    That would require the use of a query. If you're having a hard time making that work, can you edit your question to show a fragment of the actual JSON (as text, no screenshots please)? You can get this by clicking the "Export JSON" link in the overflow menu (?) on your Firebase Database console. – Frank van Puffelen Mar 17 at 22:33
  • An SQL query for this task should be a 1 liner ```SELECT TITLE FROM STATION WHERE STATION_NAME = "StationName"````. No? – mindoverflow Mar 17 at 22:35
  • Either way, I think what OP has gotten is the table name. – mindoverflow Mar 17 at 22:36
2

Selecting some nodes based on their value requires the use of a query, which is how you order and filter data in Firebase.

In this case it sounds like you want to order on the child property PROMOTED and then filter for true values, which would look like this:

Query query = ref.orderByChild("PROMOTED").equalTo(true);

query.addValueEventListener(new ValueEventListener() {
    ...

The rest of your code can stay unmodified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.