Reputation: 25
I already have a code that get the Data from Firestore Database, the data consist of Date,Invoice numb, item type, qty and price. What i want is when i show it to recyclerview, the data is sorted asscendingly based on the invoice number. anybody know how ? thanks
private void filterSearch() {
pbloading.setVisibility(View.VISIBLE);
String filter = filterDate.getText().toString();
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("sales").whereEqualTo("date",filter).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(Tag.ITEM, document.getId() + "=>" + document.getData());
String invoice = document.getString("invoice");
String date = document.getString("date");
String type = document.getString("type");
Integer qty = document.getLong("qty").intValue();
Integer price = document.getLong("price").intValue();
sales = new Sales(invoice, date, type, qty, price);
salesList.add(sales);
}
adapter = new SalesAdapter(SalesHistoryActivity.this, salesList);
recyclerView.setAdapter(adapter);
pbloading.setVisibility(View.GONE);
} else {
pbloading.setVisibility(View.GONE);
Toast.makeText(SalesHistoryActivity.this, "Error", Toast.LENGTH_SHORT).show();
Log.w(Tag.ITEM, "error getting documents", task.getException());
}
}
});
}
Upvotes: 2
Views: 481
Reputation: 77
I faced a similar problem while retrieving Image URLS from firestore in a sorted order, So in order to achieve that I had use my own logic.
Step 1: I had to make sure that my keys were in order. The retrieval only orders them by value NOT the keys. Step 2: The following code is pretty self explanatory. It gets the keys and sorts them and then another loop adds the sorted key Item to another list that is sorted
FirebaseFirestore.getInstance().collection(collection).document(doc[0]).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.getResult() != null) {
ArrayList<String> urls = new ArrayList<>();
ArrayList<String> desc = new ArrayList<>();
DocumentSnapshot doc = task.getResult();
Map<String, Object> map = doc.getData();
Object[] keysArray = map.keySet().toArray();
ArrayList<String> keysList = new ArrayList<>();
for(Object key : keysArray){
keysList.add((String)key);
}
Collections.sort(keysList);
for(String key : keysList){
urls.add(doc.getString(key));
desc.add(key);
}
viewPager2.setAdapter(setupViewPager(urls, desc));
styleViewPager();
}
}
});
Upvotes: 0
Reputation: 6277
Use Firebase's orderBy() method.
Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.
add .orderBy("invoice", Query.Direction.ASCENDING)
to your query.
db.collection("sales").whereEqualTo("date", filter).orderBy("invoice", Query.Direction.ASCENDING).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
//...
Upvotes: 4
Reputation: 3930
Try like the following.
for (QueryDocumentSnapshot document : task.getResult()) {
// ....
salesList.add(sales);
}
Collections.sort(salesList, new Comparator<Sales>(){
public int compare(Sales lhs, Sales rhs) {
return lhs.getInvoice().compareTo(rhs.getInvoice());
}
}
);
adapter = new SalesAdapter(SalesHistoryActivity.this, salesList);
Hope it helps you.
Upvotes: 1