"我的订单"在我的页面显示每个人的订单

时间:2017-09-16 15:35:26

标签: android firebase firebase-realtime-database

假设当我使用谷歌登录我的应用程序登录然后我订购食物,如果有人从其他应用程序订购食物与其他帐户如果用户订单然后我的订单可以显示他的订单和他的订单被加载到我的包括我的订单 。所以,每个用户都可以看到每个人的订单。帮我解决这个问题。

    package com.food.sheenishere.stark;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.food.sheenishere.stark.Common.Common;
import com.food.sheenishere.stark.Model.Request;
import com.food.sheenishere.stark.ViewHolder.OrderViewHolder;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class OrderStatus extends AppCompatActivity {

    public RecyclerView recyclerView;
    public RecyclerView.LayoutManager layoutManager;

    FirebaseRecyclerAdapter<Request,OrderViewHolder> adapter;
    FirebaseDatabase database;
    DatabaseReference requests;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_status);
        //firebase
        database=FirebaseDatabase.getInstance();
        requests=database.getReference("Requests");

        recyclerView= (RecyclerView)findViewById(R.id.listOrders);
        recyclerView.setHasFixedSize(true);
        layoutManager= new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        loadOrders();


    }

    private void loadOrders() {


        adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(
                Request.class,
                R.layout.order_layout,
                OrderViewHolder.class,
                requests.orderByChild("name")

        ) {



            @Override
            protected void populateViewHolder(OrderViewHolder viewHolder, Request model, int position) {
                viewHolder.txtOrderId.setText(adapter.getRef(position).getKey());
                viewHolder.txtOrderStatus.setText(convertCodeToStatus(model.getStatus()));
                viewHolder.txtOrderAddress.setText(model.getAddress());
                viewHolder.txtOrderPhone.setText(model.getPhone());
                viewHolder.txtOrderName.setText(model.getName());
            }
        };
        recyclerView.setAdapter(adapter);
    }

    private String convertCodeToStatus(String status) {
        if (status.equals("0"))
            return "placed  ";
        else if (status.equals("1"))
            return "On my way";
        else
            return "Shipped   ";
    }
}

com.food.sheenishere.stark.Model.Request

 package com.food.sheenishere.stark.Model;

import java.util.List;

/**
 * Created by WAHDAT KASHMIRI on 12-09-2017.
 */

public class Request {
    private String phone;
    private String name;
    private String address;
    private String total;
    private String status;
    private List<Order> foods; //list of food order


    public Request() {
    }


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

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public List<Order> getFoods() {
        return foods;
    }

    public void setFoods(List<Order> foods) {
        this.foods = foods;
    }
}

1 个答案:

答案 0 :(得分:0)

我建议在您的Request模型中添加userId。

private String userId;

与创建请求的用户一起填充。然后,当您检索要显示的记录时,仅userId检索当前登录用户的记录。

如果您使用的是Android Google登录,请参阅此部分文档,了解该用户的唯一标识符来源:

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount#getId()