获取Firebase结构中特定表格中的所有数据

时间:2018-06-09 04:45:41

标签: android firebase firebase-realtime-database firebase-authentication

祝你有个美好的一天。

我正在逐步使用Firebase构建我的小项目。但今天我遇到了麻烦。

我的Firebase结构如下:

enter image description here

我不知道如何获取客户订购的所有订单的数据,而无需访问密钥:“号码电话”。我一直在思考,并没有为我找到解决方案。

如果访问密钥:“NumberPhone”,然后获取此“NumberPhone”已订购的所有订单的数据已完成。非常简单。

代码如下:

这是Request和Order对象:

public class Request {
    //    private String phone, name, address, total, status;
    private String phone, name, address, status;
    private int total;
    private List<Order> foods;

    public Request() {
    }

    public Request(String phone, String name, String address, int total, List<Order> foods) {
        this.phone = phone;
        this.name = name;
        this.address = address;
        this.total = total;
        this.foods = foods;
        this.status = "0"; //Default 0. 0: place, 1: shipping, 2: shipped
    }

    //The getter and setter are definitely in the back.

public class Order {
    private String ID, UserPhone, ProductId, ProductName, Quantity, Price, Discount;

    public Order() {
    }

    public Order(String ID, String userPhone, String productId, String productName, String quantity, String price, String discount) {
        this.ID = ID;
        UserPhone = userPhone; //Phone numbers are a condition of the product search in the shopping cart of the account
        ProductId = productId;
        ProductName = productName;
        Quantity = quantity;
        Price = price;
        Discount = discount;
    }
        //The getter and setter are definitely in the back.

这是OrderViewHolder:

public class OrderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener {

    public TextView txtOrderId, txtOrderStatus, txtOrderPhone, txtOrderAddress, txtOrderTotal;
    private ItemClickListener itemClickListener;

    public OrderViewHolder(View itemView) {
        super(itemView);

        txtOrderId = (TextView) itemView.findViewById(R.id.order_id);
        txtOrderStatus = (TextView) itemView.findViewById(R.id.order_status);
        txtOrderPhone = (TextView) itemView.findViewById(R.id.order_phone);
        txtOrderAddress = (TextView) itemView.findViewById(R.id.order_address);
        txtOrderTotal = (TextView) itemView.findViewById(R.id.order_total);

        itemView.setOnClickListener(this);
        itemView.setOnCreateContextMenuListener(this);
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onClick(View view) {
        itemClickListener.onClick(view,getAdapterPosition(),false);
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        contextMenu.setHeaderTitle("Select the action");
        contextMenu.add(0, 0, getAdapterPosition(), Common.UPDATE);
        contextMenu.add(0, 1, getAdapterPosition(), Common.DELETE);
    }
}

添加这是代码main:

    private FirebaseRecyclerAdapter<Request, OrderViewHolder> adapter;

private void loadOrders() {
        adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(
                Request.class,
                R.layout.order_item,
                OrderViewHolder.class,
                requests.child(phone)
        ) {
            @Override
            protected void populateViewHolder(OrderViewHolder viewHolder, final Request model, int position) {
                viewHolder.txtOrderId.setText(adapter.getRef(position).getKey());
                viewHolder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
                viewHolder.txtOrderPhone.setText(model.getPhone());
                viewHolder.txtOrderAddress.setText(model.getAddress());
                viewHolder.txtOrderTotal.setText(String.valueOf(model.getTotal()));
                viewHolder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
////                        Get CategoryID and send Activity Foods List:
//                        Intent moveToOrderDetails = new Intent(OrderStatusActivity.this, OrderDetailsActivity.class);
//                        moveToOrderDetails.putExtra("KeyOrder", adapter.getRef(position).getKey());
//                        startActivity(moveToOrderDetails);
                        Toast.makeText(OrderStatusActivity.this, model.getName(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        adapter.notifyDataSetChanged();
        recyclerView.setAdapter(adapter);

我遇到困难的事情是不知道如何获取所有订单的数据而无需访问密钥:“NumberPhone”。我一直在思考,无法为我找到解决方案。

我非常尊重某人的想法。我需要想法,解决方案。我非常感谢!

0 个答案:

没有答案