嘿我正在尝试按升序对数据进行排序,但数据会按照它们在数据库中的存储顺序显示:
这是用于对数据进行排序以找到最短时间的函数。
private void getTimeTaken1(final String name) {
final DatabaseReference databaseReference1 = FirebaseDatabase.getInstance().getReference().child("users");
final Query query = databaseReference1.orderByChild("timeTaken").limitToLast(10);
Log.e(TAG, "getTimeTaken1: " + name);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int rank = 1;
for (DataSnapshot userAnswerSnapshot : dataSnapshot.getChildren()) {
if (yourTimeArraylist.size() < 10) {
if (userAnswerSnapshot.child("questions").child(name).hasChild("timeTaken")
&& userAnswerSnapshot.child("questions").child(name).child("checkAnswerCorrect").getValue(String.class).equals("1")) {
Log.e("", "User " + " " + userAnswerSnapshot.child("questions").child(name).child("timeTaken").getValue().toString());
yourTimeArraylist.add(new PreviousDayRank(userAnswerSnapshot.child("random").getValue(String.class),
userAnswerSnapshot.child("name").getValue(String.class),
timetaken(userAnswerSnapshot.child("questions").child(name).child("timeTaken").getValue(Integer.class)),
rank));
Log.e("", "onDataChange: user" + userAnswerSnapshot.child("questions").child(name).child("timeTaken").getValue());
rank++;
} else {
yourTimeArraylist.add(new PreviousDayRank(userAnswerSnapshot.child("random").getValue(String.class), userAnswerSnapshot.child("name").getValue(String.class), "-", rankl̥));
rank++;
}
}
}
query.removeEventListener(this);
databaseReference1.removeEventListener(this);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
答案 0 :(得分:1)
您的DatabaseReference
错了。请使用此代码:
final DatabaseReference databaseReference1 = FirebaseDatabase.getInstance().getReference().child("users").child(userId).child("questions").child(questionNumber);
final Query query = databaseReference1.orderByChild("timeTaken").limitToLast(10);
其中userId
和questionNumber
是由push()
方法生成的ID,第二个由您生成。
希望它有所帮助。