我正在从firebase数据库中检索帖子数据。检索到的数据包括我的帖子以及我关注的用户的帖子。一切都很顺利,但我想根据我在每个帖子中存储的时间和日期对检索到的数据进行排序。我尝试了很多解决方案,但我无法实现我的愿望。
这是我的代码:
//For retrieving the detail of current user posts and setting it in custom listview through DataModel class
com.google.firebase.database.ValueEventListener eventListener = new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
for (com.google.firebase.database.DataSnapshot ds : dataSnapshot.getChildren()) {
String description = ds.child("description").getValue(String.class);
String dateTime = ds.child("dateTime").getValue(String.class);
String posturi = ds.child("posturi").getValue(String.class);
String severity = ds.child("severity").getValue(String.class);
String lat = ds.child("lat").getValue(String.class);
String lon = ds.child("lon").getValue(String.class);
DataModel dataModel = new DataModel(userID, name, dateTime, description, profileImage, posturi, lon, lat, severity);
list.add(dataModel);
}
adapter = new CustomAdapter(Home.this, list);
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("DataBase Error", databaseError.toString());
}
};
databaseref_currentuser_post.orderByChild("dateTime").addListenerForSingleValueEvent(eventListener);
//检索我关注的用户的数据。
databaseref_otheruser_post = databaseReference.child(id).child("posts");
databaseref_otheruser_post.orderByChild("dateTime").addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
for (com.google.firebase.database.DataSnapshot ds : dataSnapshot.getChildren()) {
String description = ds.child("description").getValue(String.class);
String dateTime = ds.child("dateTime").getValue(String.class);
String posturi = ds.child("posturi").getValue(String.class);
String severity = ds.child("severity").getValue(String.class);
String lat = ds.child("lat").getValue(String.class);
String lon = ds.child("lon").getValue(String.class);
DataModel dataModel1 = new DataModel(id, othername, dateTime, description, picture, posturi, lon, lat, severity);
list.add(dataModel1);
}
adapter = new CustomAdapter(Home.this, list);
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
提前感谢!