问题
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]
回答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.
回答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!
来源:https://stackoverflow.com/questions/62867191/retrieve-data-of-specific-value-in-firebase-recycle-view-in-android-studio