2017-08-14 12 views
0

firebase snapshot火元基準の合計金額の計算

2つの日付間の合計所得(0 + 500)を計算する必要があります。

//これは私のアンドロイドコード

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

        } 

    } 
    }); 

//私は2つの日付の間の子供の収入の合計を計算する必要があります。、すべての

答えて

3

まず、timestamp.Thisとしてあなたの日付を格納しています選択と減算の実行に役立ちます。

次に、タイムスタンプを格納するこの変数に基づいてFirebaseのインデックスを作成します。

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. 
    } 
}); 
+0

...、それを実装する...、うまく説明.. –

+0

YOUR_TIMESTAMP_VARIABLEに含まれるいただきました...親切にそれはより多くのビット説明...、 –

+3

@VincentMacharia:あなたの現在のデータ構造のためのものが含まれています'11:29:35'のようなものです。しかしKennyが最初の行で述べたように、タイムスタンプなどのクエリに適した形式で日付/時刻を保存する必要があります。 https://stackoverflow.com/questions/37976468/saving-and-retrieving-date-in-firebase –

関連する問題