我有一个排行榜,可以找回前十名球员。数据在第一次调用时正确显示,但每当onPause / onStop然后onResume / onStart被调用时,数据就不再按其排序顺序显示,就好像orderByValue()
方法被忽略一样。我在onStart()
方法上附加了侦听器,并在onStop()
方法中将其删除。这让我发疯了。
//get the active week id to use for reference
String week_id = getActivity().getSharedPreferences(Constants.getPreferenceFile(),
Context.MODE_PRIVATE).getString(Constants.getActiveWeek(),null);
//initialize the database references
if(leader_board_child_event_listener == null) {
leader_board_reference = MainActivity.firebaseDatabase.getRef().child(Constants.getLeaderBoardChild());
user_reference = MainActivity.firebaseDatabase.getRef().child(Constants.getUsersChild());
Log.d(TAG, "sorting the query first");
leader_board_query = leader_board_reference.child(week_id).orderByValue().limitToLast(10);
leader_board_child_event_listener = new ChildEventListener() {
@Override
public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "child added == " + dataSnapshot.getValue());
//get the user details every time child added is called
user_reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot2) {
hideProgressBar();
//add the user details to a list
user_list.add(new User(dataSnapshot.getKey(), dataSnapshot2.child(dataSnapshot.getKey()).
child(Constants.getUserNameChild()).getValue(String.class), dataSnapshot2.
child(dataSnapshot.getKey()).child(Constants.getProfilePicUriChild()).getValue(String.class),
dataSnapshot.getValue(double.class)));
//update the recyclerview adapter with each addition
board_adapter.notifyDataSetChanged();
Log.d(TAG, "user details: " + user_list.size());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@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) {
}
};
}
//add the listener to leader board database reference
leader_board_query.addChildEventListener(leader_board_child_event_listener);
我做错了什么?