Firebase根据日期进行排序 [英] Firebase sort by points depending on date
问题描述
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference highscoreRef = database.getReference();
$ b //以字符串形式给scoreList和scoreList
highscoreRef.orderByChild(score)。limitToLast( 10).addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot snapshot,String previousChildKey){
try {
nameList.push(snapshot .child(name)。getValue()。toString());
scoreList.push(snapshot.child(score)。getValue()。toString());
)catch(Exception e){
//Toast.makeText(getApplicationContext(),Error fetching data。,Toast.LENGTH_SHORT).show();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot){
}
@Override
pub我们可以使用数据快照(DataSnapshot dataSnapshot,String s){
}
@Override $ b $ public void onCancelled(DatabaseError databaseError){
Toast.makeText(getApplicationContext(), 发送数据错误,Toast.LENGTH_LONG).show();
}
});
如果您想查询日期范围,他们在一个允许查询范围的格式。日期可以是时间戳或按字典顺序排序的字符串。
您目前将日期存储为 dd.MM.yyyy
字符串,该字符串不是按字典顺序排序的。我建议切换到 yyyy-MM-dd
,所以 2019-01-09
。这样,如果你想获得1月份的所有帖子,你可以做一个范围查询:
$ b $ pre $ ref $ orderByChild(date ).startAt(2017-01-01)。endAt(2017-01-31)
<如果你已经知道你想每月排行榜,我建议改变你的数据库结构来反映。因此:按照他们所在月份存储分数:
2017-01
-K34761761238712
名称:GGGG
得分:3
-K4875731941298a
名称:AAA
得分:1
$ b $ p这样你就可以得到1月份的高分: ref.child(2017-01)。orderByChild(score)。limitToLast(10)
I am trying to add a monthly scoreboard but it seems to be a little bit difficult to me. I don't know how to just take a specific value to some list or array. Like Date, only date with 09.01.2017 values. If I could do that then I think that I can sort them by value.
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference highscoreRef = database.getReference();
// Ordering with score and adding key values as string to nameList and scoreList
highscoreRef.orderByChild("score").limitToLast(10).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
try {
nameList.push(snapshot.child("name").getValue().toString());
scoreList.push(snapshot.child("score").getValue().toString());
} catch (Exception e) {
//Toast.makeText(getApplicationContext(), "Error fetching data.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), "Error sending data.", Toast.LENGTH_LONG).show();
}
});
If you want to query date ranges, you should store them in a format that allows querying ranges. For dates that would be either a timestamp or a string that is lexicographically sortable.
You currently store dates as a dd.MM.yyyy
string, which is not lexicographically sortable. I recommend switching to yyyy-MM-dd
, so 2019-01-09
. That way if you want to get all posts for January, you can do a range query:
ref.orderByChild("date").startAt("2017-01-01").endAt("2017-01-31")
If you already know that you want monthly leaderboard, I'd recommend changing your database structure to reflect that. So: store the scores by the month that they're in:
2017-01
-K34761761238712
name: "GGGG"
score: 3
-K4875731941298a
name: "AAA"
score: 1
That way you can get the high scores for January in order with:
ref.child("2017-01").orderByChild("score").limitToLast(10)
这篇关于Firebase根据日期进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!