{
"YoZSiyACIqSBNXU7XKv56olKOv92":
{
"-KpJVrAUg9yCPNptU-jY":
{
"cmt": "hello",
"ids": "-KpJVrAIVocWckK-vEd2",
"imgid": "-KpJT3Ygd8U3yf9nN-Yr",
"timestamp": "1500361488354",
"userid": "surabhipaul.gkp2008@gmail.com"
},
"-KpoR8S99tMm1fDmlwaR":
{
"cmt": "really",
"ids": "-KpoR8S99tMm1fDmlwaQ",
"imgid": "-KpJT3Ygd8U3yf9nN-Y,
"timestamp": "1500897125323",
"userid": "surabhipaul.gkp2008@gmail.com"
}
},
"pTMeNLkIHqPmFOCPQcK2yjImdmi2":
{
"-KpU8HxLfohEtXIGJzi9":
{
"cmt": "hie",
"ids": "-KpU8HxLfohEtXIGJzi8",
"imgid": "-KpJT3Ygd8U3yf9nN-Yr",
"timestamp": "1500539860639",
"userid": "sanchisrivastava123@gmail.com"
},
"-KpoQo6615Y-U-DQ_Le1":
{
"cmt": "nice",
"ids": "-KpoQo6-flNhGxZd4V19",
"imgid": "-KpJT3Ygd8U3yf9nN-Yr",
"timestamp": "1500897037891",
"userid": "sanchisrivastava123@gmail.com"
}
}
}
如何根据时间戳检索数据,以便在recyclerview中首先显示具有最新时间戳的数据。
我的代码是:
myref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<>();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
for(DataSnapshot data2 :dataSnapshot1.getChildren()){
Comment userdetails = data2.getValue(Comment.class);
final CommentData listdata = new CommentData();
final String cmt=userdetails.getCmt();
final String user = userdetails.getUserid();
final String cmtid = userdetails.getIds();
listdata.setNos(String.valueOf(c));
listdata.setCmt(cmt);
listdata.setUserid(user);
listdata.setComid(cmtid);
list.add(listdata);
RecyclerCommentAdapter recycler = new RecyclerCommentAdapter(list);
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getApplicationContext());
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator( new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
请告诉我如何根据来自firebase数据库的时间戳首先在recyclerview上显示最新数据。 这是CommentData类:
public class CommentData {
String cmt;
String userid;
String comid;
String nos;
public String getNos() {
return nos;
}
public void setNos(String nos) {
this.nos = nos;
}
public String getComid() {
return comid;
}
public void setComid(String comid) {
this.comid = comid;
}
public String getCmt() {
return cmt;
}
public void setCmt(String cmt) {
this.cmt = cmt;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
}
答案 0 :(得分:0)
解决方案非常棘手,您需要将推送的密钥更改为时间戳* -1
例如:
"YoZSiyACIqSBNXU7XKv56olKOv92":
{
"-1500897125323":
{
"cmt": "really",
"ids": "-KpoR8S99tMm1fDmlwaQ",
"imgid": "-KpJT3Ygd8U3yf9nN-Y,
"timestamp": "1500897125323",
"userid": "surabhipaul.gkp2008@gmail.com"
},
"-1500361488354":
{
"cmt": "hello",
"ids": "-KpJVrAIVocWckK-vEd2",
"imgid": "-KpJT3Ygd8U3yf9nN-Yr",
"timestamp": "1500361488354",
"userid": "surabhipaul.gkp2008@gmail.com"
}
}
答案 1 :(得分:0)
Collections.sort(list, new Comparator<CommentData>() {
@Override
public int compare(CommentData o1, CommentData o2) {
return Long.compare(Long.parseLong(o2.time),
Long.parseLong(o1.time));
}
});