Skip to content
Advertisement

showing only certain items from a firebase database node

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);
            }
        }

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement