firebase
的数据时,我都会遇到问题。它显示在recyclerview
中,但recyclerview
中的排列必须是最新日期。
日期2/1/2018必须是列表中的第一个。以下是我如何选择数据的代码。
myRef = FirebaseDatabase.getInstance().getReference("complaints");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try{
list = new ArrayList<Blog>();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
Blog value = dataSnapshot1.getValue(Blog.class);
Boolean Posted = value.getPosted();
String complaint_type = value.getComplaintType();
if (Posted== true && complaint_type.equals("Civic Complaint")) {
int ComplaintNo = value.getComplaintNo();
String Description = value.getDescription();
String Date = value.getDate();
String MediaURL = value.getMediaURL();
String Time = value.getTime();
String Address = value.getAddress();
int count_satisfiedd = value.getCount_Satisfied();
int count_dissatisfied = value.getCount_Dissatisfied();
boolean posted = value.getPosted();
Blog fire = new Blog();
fire.setComplaintNo(ComplaintNo);
fire.setDescription(Description);
fire.setDate(Date);
fire.setMediaURL(MediaURL);
fire.setTime(Time);
fire.setAddress(Address);
fire.setPosted(posted);
fire.setCount_Satisfied(count_satisfiedd);
fire.setCount_Dissatisfied(count_dissatisfied);
list.add(fire);
}
}
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(list,getActivity());
RecyclerView.LayoutManager recyce = new GridLayoutManager(getActivity(),1);
recyclerView.setLayoutManager(recyce);
recyclerView.setItemAnimator( new DefaultItemAnimator());
recyclerView.setAdapter(recyclerAdapter);
}catch (Exception e){
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是数据库:
"complaints" : [ null, {
"Address" : "Earth Street ",
"CivicCategory" : "",",
"ComplaintNo" : 1,
"ComplaintType" : "",
"Date" : "02 / 01 /2018",
"Description" : ".",
"MediaType" : "Image",
"NearIn" : "Palengke ",
"Posted" : true,
"Time" : "11:42 AM"
}, {
"Address" : "Mars Street ",
"CivicCategory" : " ",
"ComplainantID" : "voter1",
"ComplaintNo" : 2,
"ComplaintType" : "",
"Count_Resolved" : 0,
"Count_Solved" : 0,
"Date" : "01 / 03 /2018",
"Description" : "
"Posted" : true,
"Time" : "11:43 AM"
} ],
我怎样才能得到预期的结果?
感谢您的帮助
答案 0 :(得分:0)
您可以在将列表传递给适配器之前将其反转
Collections.reverse(yourList); // <---- reverse the list
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(list,getActivity());
RecyclerView.LayoutManager recyce = new GridLayoutManager(getActivity(),1);
recyclerView.setLayoutManager(recyce);
recyclerView.setItemAnimator( new DefaultItemAnimator());
recyclerView.setAdapter(recyclerAdapter);
答案 1 :(得分:0)
我想,您可以采用以下两种方式之一,
单向 -
在arraylist中插入值后,按“date”键对其进行排序
Collections.sort(arrayList, new Comparator<ClassObject>() {
@Override
public int compare(ClassObject o1, ClassObject o2) {
// here you need to convert string to date format
return o1.getDate().compareTo(o2.getDate());
}
});
另一种方式 -
在firebase数据库中插入数据时,使用timestamp格式或按字典顺序排序的字符串,因为firebase数据库按字典顺序排序。 (当前“日期”值格式不是按字典顺序排序的。
然后你的查询就像,
ref.orderByChild("Date")
我附上了一张用“timestamp”键的图片。我正在使用这个获得排序结果。
根据您的结构,您可能需要做一些合乎逻辑的事情。