molleman
molleman

Reputation: 2946

How to query Firebase database and not show values based of a boolean value

I am using FirebaseListAdapter in Android to display a list of Orders based off a certain date:

Query orderQuery = ordersRef.orderByChild("dateCreated").startAt(d1.getTime());


My Orders have a completed boolean property

-KsRHO1sLcVxKtVMec3o
      completed: false
      dateCreated: 1503713136950

I would like to only show not completed items in my list adapter. the current query above retrieves all orders.

The issue is I don’t know how to query the database correctly to get this to work.

The only idea I have is in the populateView method of the FirebaseListAdapter to check if(!model.isCompleted()){ } and not fill in the textViews associated with the list item.

What way could I achieve my desired result?

Upvotes: 1

Views: 252

Answers (2)

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

You can try to use different references for completed and incomplete orders.

Initially when order is placed it will be in the incomplete state and when your order is updated then move the order with key in the complete reference.

Step 1: Initially when order is getting placed, store the order in a "incomplete" reference like:

-incomplete:{
    -anyRandomKeyGeneratedByFirebase:{
       "orderTitle":"My First Order"... your order detail goes here
       "orderKey":"anyRandomKeyGeneratedByFirebase"
     },
     .
     .
     .
}

Above structure will be for incomplete order.

When you know that your order status is completed then you will have to move order into the complete child reference like:

-complete:{
        -anyRandomKeyGeneratedByFirebase:{
           "orderTitle":"My First Order"... your order detail goes here
           "orderKey":"anyRandomKeyGeneratedByFirebase"
         },
         .
         .
         .
    }

So in this it will solve your problem. Rather than fetching all orders fetch only completed orders from complete reference.

-orders:{
  -incompleteOrders:{
  },
  -completeOrders:{
  }
}

Upvotes: 0

Elsunhoty
Elsunhoty

Reputation: 1627

in firebase there is not Multi Select or "Multi Query"

so you can orderByChild creating time Or compeleted

So you have to use the Method You mention in your Question

    The only idea I have is in the populateView method of the
 FirebaseListAdapter to check 
if(!model.isCompleted()){ } and not fill in the textViews associated with the list item.

or you have to edit your database Structure

-completed
 -KsRHO1sLcVxKtVMec3o
       dateCreated: 1503713136950

and

 -Notcompleted
     -KsRHO1sLcVxKtVMec3o
           dateCreated: 1503713136950

like when it`s completed .. remove it from notcompleted Node and add it in completed node

Upvotes: 1

Related Questions