我可以添加食物,orderi地址和电话号码信息,并在Firebase的请求/ ID下查看所有这些信息。但是,当我点击导航抽屉以及主菜单中的订单和购物车按钮时,我无法列出我添加到购物车中的食物。查询部分似乎存在问题。请帮我做点什么
OrderStatus.java
public class OrderStatus extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference requests;
FirebaseRecyclerAdapter<Request, OrderViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_status);
//firebase
database = FirebaseDatabase.getInstance();
requests = database.getReference("Request");
recyclerView = findViewById(R.id.listOrders);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
loadOrders(user.getEmail().toString());
}
private void loadOrders(String email) {
Query query = FirebaseDatabase.getInstance()
.getReference("Request")
.orderByChild("email")
.startAt(email.toString()).equalTo(true,email)
.limitToLast(50);
FirebaseRecyclerOptions<Request> options = new FirebaseRecyclerOptions.Builder<Request>().setQuery(query, Request.class).build();
adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(options) {
@NonNull
@Override
public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_layout, parent, false);
return new OrderViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull OrderViewHolder viewHolder, int position, @NonNull Request model) {
viewHolder.txtOrderID.setText(adapter.getRef(position).getKey());
viewHolder.txtOrderStatus.setText(convertCarttoStatus(model.getStatus()));
viewHolder.txtOrderAddress.setText(model.getAddress());
viewHolder.txtOrderPhone.setText(model.getPhone());
}
};
recyclerView.setAdapter(adapter);
}
private String convertCarttoStatus(String status) {
if (status == "0")
return "PLaced";
else if (status == "1")
return "On My Way";
else
return "Shipped";
}
答案 0 :(得分:0)
这很有用。如果您需要,可以使用
public class OrderStatus extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference requests;
FirebaseRecyclerAdapter<Request, OrderViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_status);
//firebase
database = FirebaseDatabase.getInstance();
requests = database.getReference("Request");
recyclerView = findViewById(R.id.listOrders);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
phoneNumber = getIntent().getStringExtra("phoneNumber");
loadOrders(phoneNumber);
}
private void loadOrders(String email) {
Query query = FirebaseDatabase.getInstance()
.getReference("Request")
.limitToLast(50);
FirebaseRecyclerOptions<Request> options = new FirebaseRecyclerOptions.Builder<Request>().setQuery(query, Request.class).build();
adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(options) {
@NonNull
@Override
public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_layout, parent, false);
return new OrderViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull OrderViewHolder viewHolder, int position, @NonNull Request model) {
viewHolder.txtOrderID.setText(adapter.getRef(position).getKey());
viewHolder.txtOrderStatus.setText(convertCarttoStatus(model.getStatus()));
viewHolder.txtOrderAddress.setText(model.getAddress());
viewHolder.txtOrderPhone.setText(model.getPhone());
}
};
recyclerView.setAdapter(adapter);
}
protected void onStart() {
super.onStart();
adapter.startListening();
}
protected void onStop() {
super.onStop();
adapter.stopListening();
}
private String convertCarttoStatus(String status) {
if (status == "0")
return "PLaced";
else if (status == "1")
return "On My Way";
else
return "Shipped";
}