1 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
我能想到的最簡(jiǎn)單的解決方案是在每個(gè)用戶對(duì)象下添加總步數(shù)作為新屬性。在這種情況下,您還需要做一件事,即根據(jù)您每天獲得的步數(shù)增加該屬性。您的新架構(gòu)應(yīng)如下所示:
Firebase-root
|
--- user
|
--- userId
|
--- steps
| |
| --- //daily steps
|
--- target: 100
|
--- username: "John Doe I"
|
--- totalNumberOfSteps: 111
要根據(jù)totalNumberOfSteps屬性按(升序)順序排列用戶,請(qǐng)使用以下代碼行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("user");
Query query = userRef.orderByChild("totalNumberOfSteps");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String username = ds.child("username").getValue(String.class);
long totalNumberOfSteps = ds.child("totalNumberOfSteps").getValue(Long.class);
Log.d(TAG, username + " - Steps: " + totalNumberOfSteps);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);
然后按降序排列,請(qǐng)從以下帖子中查看我的回答:
如何按升序或降序排列firebase數(shù)據(jù)庫(kù)數(shù)據(jù)?
最后,logcat 中的結(jié)果將是:
John Doe II - Steps: 111
John Doe IV - Steps: 88
John Doe I - Steps: 79
John Doe V - Steps: 12
John Doe III - Steps: 0
John Doe VI - Steps: 0
添加回答
舉報(bào)