Solution 1 :
Try it this way.
Assuming that AdminOrders.class
is your model class.
{
FirebaseRecyclerAdapter<AdminOrders, AdminNewOrdersActivity.AdminOrderViewHolder> adapter;
FirebaseRecyclerOptions<AdminOrders> options =
new FirebaseRecyclerOptions.Builder<AdminOrders>()
.setQuery(shippedOrdersRef.orderByChild("state").equalTo("shipped"), AdminOrders.class)
.build();
adapter = new FirebaseRecyclerAdapter<Products_Model, OrderProductsFragment.AdminOrderViewHolder>(options){
@SuppressLint("SetTextI18n")
@Override
protected void onBindViewHolder(@NonNull final AdminNewOrdersActivity.AdminOrderViewHolder holder, final int position, @NonNull final AdminOrders model) {
// Set your elements here
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view, parent, false);
return new AdminOrderViewHolder(view);
}
@NonNull
@Override
public AdminOrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//Set your view here
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
Sorry for the alignments and the brackets if I have mistaken.
Solution 2 :
One way is like this (you have an if-statement and you store the ones you want in an array):
List<String> shipped = new ArrayList<>();
shippedOrdersRef = FirebaseDatabase.getInstance().getReference().child("Orders");
shippedOrdersRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
Orders order = ds.getValue(Orders.class);
if(order.getState().equals("shipped")) shipped.add(order.getState());
}
});
Where Orders is the name of the class with the constructor of the orders.
Another way, faster, is this:
shippedOrdersRef = FirebaseDatabase.getInstance().getReference().child("Orders");
shippedOrdersRef.orderByChild("state").equalTo("shipped").addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
DataSnapshot firstChild = dataSnapshot.getChildren().iterator().next();
//your wanted value is firstChild.getKey()
});
Here is a helpful link.
Hope I helped!
Problem :
i have an e-commerce android app and i have all orders(state = shipped & not shipenter image description hereped) which are received by admin in recycler view from Firebase realtime database. Now, i want to get shipped orders(state = shipped) in another activity and i dont know how to retrieve only those orders which have state = shipped. I have tried several things but cant get around this. So anyone could help please answer.
My recyclerView code looks this this:
shippedOrdersRef = FirebaseDatabase.getInstance().getReference().child("Orders")
FirebaseRecyclerOptions<AdminOrders> options = new FirebaseRecyclerOptions.Builder<AdminOrders>()
.setQuery(shippedOrdersRef, AdminOrders.class).build();
FirebaseRecyclerAdapter<AdminOrders, AdminNewOrdersActivity.AdminOrderViewHolder> adapter =
new FirebaseRecyclerAdapter<AdminOrders, AdminNewOrdersActivity.AdminOrderViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull AdminNewOrdersActivity.AdminOrderViewHolder holder, final int i, @NonNull AdminOrders adminOrders)
{
holder.userName.setText("Name: " + adminOrders.getName());
holder.userPhoneNumber.setText("Phone: " + adminOrders.getPhone());
holder.userTotalPrice.setText("Total Amount = Rs." + adminOrders.getTotalAmount());
holder.userDateTime.setText("Order at: " + adminOrders.getDate() + " " + adminOrders.getTime());
holder.userShippingAddress.setText("Shipping Address: " + adminOrders.getAddress() + ", " + adminOrders.getCity());
holder.orderState.setText("" + adminOrders.getState());
}
@NonNull
@Override
public AdminNewOrdersActivity.AdminOrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.orders_layout, parent, false);
return new AdminNewOrdersActivity.AdminOrderViewHolder(view);
}
};
shippedOrdersList.setAdapter(adapter);
adapter.startListening();
}
[My database looks like this][1]
Comments
Comment posted by i.stack.imgur.com/qhac2.png
Comment posted by Muhammad Hamza Munir
this is picture of my database
Comment posted by Muhammad Hamza Munir
i didn’t understand how to use this query and where.
Comment posted by KalanaChinthaka
I am sorry. I updated the answer. Please change the values if I have missed some since this is something I used for my current projects.
Comment posted by Muhammad Hamza Munir
i want to do it using second way so where do i add this code inside recyclerview adapter or where?
Comment posted by LoukasPap
It depends on which screen you want to show them up. You say you want the shipped orders in another activity, so put it in your other class(activity). Is the code above, the activity in which you want to take the shipped states?
Comment posted by LoukasPap
Add ‘orderByChild(“state”).equalTo(“shipped”)’ next to ‘shippedOrdersRef = FirebaseDatabase.getInstance().getReference().child(“Orders”)’