Reputation: 55
I am making a Sweet order like App, I want to sort Orders according to order status like Placed(0), Shipped(1), OnTheWay(2), Delivered(3). If order status is 3(Delivered) I need to show that order at the bottom of the recyclerview, Currently I am using Stack Format to view orders, is it possible?
private void loadOrders(final String phone) {
adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(
Request.class,
R.layout.order_layout,
OrderViewHolder.class,
requests.orderByChild("phone").equalTo(phone)
) {
@Override
protected void populateViewHolder(OrderViewHolder orderViewHolder, final Request request, final int i) {
final String OrderId, OrderPhone, OrderTotal, OrderAddress, OrderComment, OrderUserName, OrderTime, OrderDate;
OrderId = adapter.getRef(i).getKey();
OrderPhone = request.getPhone();
OrderStatus = Common.convertCodeToStatus(request.getStatus());
OrderTotal = request.getTotal();
OrderAddress = request.getAddress();
OrderComment = request.getComment();
OrderUserName = request.getName();
OrderTime = request.getTime();
OrderDate = request.getDate();
if (request.getStatus().equals("3")){
orderViewHolder.orderStatusImage.setImageResource(R.mipmap.cupcake);
orderViewHolder.orderStatusImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
orderViewHolder.txtOrderStatus.setVisibility(View.GONE);
orderViewHolder.txtOrderDate.setVisibility(View.GONE);
orderViewHolder.txtOrderTime.setVisibility(View.GONE);
orderViewHolder.deliveredStatus.setVisibility(View.VISIBLE);
}
orderViewHolder.txtOrderId.setText(String.format("Order Id : %s",OrderId));
orderViewHolder.txtOrderStatus.setText(String.format("Order Status : %s",OrderStatus));
orderViewHolder.txtOrderTime.setText(String.format("Order Time : %s",OrderTime));
orderViewHolder.txtOrderDate.setText(String.format("Order Date : %s",OrderDate));
if (dialog.isShowing()){
dialog.dismiss();
}
//Prevention for app crash when user clicks
orderViewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Intent orderDetail = new Intent(getActivity(), OrderDetail.class);
orderDetail.putExtra("OrderId",OrderId)
.putExtra("OrderPhone",phone)
.putExtra("OrderTotal",OrderTotal)
.putExtra("OrderAddress",OrderAddress)
.putExtra("OrderComment",OrderComment)
.putExtra("OrderUserName",OrderUserName)
.putExtra("OrderTime",OrderTime)
.putExtra("OrderDate",OrderDate)
.putExtra("OrderStatus",request.getStatus());
startActivity(orderDetail);
}
});
}
};
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
Upvotes: 0
Views: 151
Reputation: 98
If I understand correctly, what you want to do is to order the Orders in 0, 1, 2, 3 sequence.
Option 1: Sort the data before rendering the list. For example:
...
adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(
Request.class,
R.layout.order_layout,
OrderViewHolder.class,
requests.orderByChild("status").equalTo(phone)
)
...
By ordering the data with status requests.orderByChild("status").equalTo(phone)
, you list will show the Orders with status sequence. Of cause, with this change, your list is not ordering by phone number now. Please provide more of your code if you want to sort by both phone and status.
Option 2: If you can manipulate the data from firebase before creating the adapter, you can split the result into 2 list, one with status 0,1,2 and the other one with status 3. After that, you add the 2 list into the adapter one by one and attach the adaptor to the recycler view.
Inside bottom of the Recyclerview just like Amazon/Flipkart Delivered Orders are shown at the bottom
So in this case, can you go with option 2. You can do 2 queries to firebase:
.orderByChild("phone").equalTo(phone)
.orderByChild("phone").equalTo(phone)
Then you append the 2 list into 1 list and create the adapter.
To optimise it further, you can query 1 time only and split the list to 2 list mentioned above locally in order not to query twice.
Upvotes: 2