0

firebase snapshot

I need to calculate the total income between two dates(0+500).

//This is my android code

mDatabase.child(selected_account).orderByChild("Date").startAt(begin_date).endAt(end_date).

        addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot snapshot, String s) {



Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
                    if (newPost != null) {

                        if (newPost.get("Account") != null & newPost.get("Account") != "") {
                            foodList.add(new TransactionFlow
                                    (newPost.get("Account").toString(),
                                            newPost.get("Amount").toString(),
                                            newPost.get("CashAtHand").toString(),
                                            newPost.get("Variant").toString(),
                                            newPost.get("Date").toString(),
                                            newPost.get("DateTime").toString(),
                                            newPost.get("Description").toString(),
                                            newPost.get("Income").toString(),
                                            newPost.get("Field").toString(),
                                            newPost.get("Organisations").toString(),

                                            newPost.get("Field").toString(),
                                            newPost.get("key").toString(),


                                            snapshot.getKey().toString()));
                        }

                    }

    }
    });

//I need to calculate the total of child income between two dates.,

Vincent Macharia
  • 475
  • 2
  • 7
  • 20

1 Answers1

3

First of all, store your date as a timestamp.This will help you perform selections and subtractions.

Secondly, Index your Firebase based on this variable that stores your timestamp.

Long totalAmount = 0l;
mDatabase.child(selected_account).orderByChild(YOUR_TIMESTAMP_VARIABLE).startAt(YOUR_BEGIN_DATE_TIMESTAMP).endAt(YOUR_END_DATE_TIMESTAMP)
 .addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
   if (dataSnapshot.getValue() != null) {
    for (DataSnapshot child: snapshot.getChildren()) {
     totalAmount += dataSnapshot.child('Income').getValue(Long.class);
    }
    //Once you finish your loop. You can present the result of totalAmount
   }
  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
//Log your error here.
  }
 });
Kennedy Nyaga
  • 3,455
  • 1
  • 26
  • 25
  • ...,Implementing it...,well explained.., – Vincent Macharia Aug 14 '17 at 12:35
  • Whats contained in YOUR_TIMESTAMP_VARIABLE...kindly explain it bit more..., – Vincent Macharia Aug 14 '17 at 12:48
  • 3
    @VincentMacharia: for your current data structure those would contains something like `11:29:35`. But as Kenny said in his first line, you should store the date/time in a format that is more suited for querying, such as a timestamp. See https://stackoverflow.com/questions/37976468/saving-and-retrieving-date-in-firebase – Frank van Puffelen Aug 14 '17 at 14:07
  • @Kenny: Secondly, Index your Firebase based on this variable that stores your timestamp......,kindly explain on this...., – Vincent Macharia Aug 15 '17 at 07:54
  • .indexOn to tell Firebase to optimize queries on the timestamp path. Helps you cut on bandwidth costs of performing a search. You can read on IndexOn here. https://firebase.google.com/docs/database/security/indexing-data – Kennedy Nyaga Aug 15 '17 at 08:04