Reputation: 304
I want to sort my data from oldest to newest from Cloud Firestore this is my code:
private void loadNotesList() {
firestoreDB.collection("keluhan")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<keluhanModel> notesList = new ArrayList<>();
for (DocumentSnapshot doc : task.getResult()) {
keluhanModel note = doc.toObject(keluhanModel.class);
note.setId(doc.getId());
notesList.add(note);
}
mAdapter = new adapterKeluhan(notesList, getApplicationContext(), firestoreDB);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}
I have tried to use .orderBy("date", Query.Direction.ASCENDING) but it didn't do anything it just a white page in my Android application.
This is my database:
Upvotes: 0
Views: 784
Reputation: 138824
To be able to order your results by date, you should use the following query:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference keluhanRef = rootRef.collection("keluhan");
Query dateQuery = keluhanRef.orderBy("timeStamp", Direction.ASCENDING);
dateQuery.get().addOnCompleteListener(/* ... */);
Calling:
.orderBy("date", Query.Direction.ASCENDING)
Will never work since your property name is timeStamp
and not date
.
Upvotes: 1