如何在Firebase中获取数据

时间:2018-07-06 06:12:23

标签: android firebase firebase-realtime-database

[Firebase数据库] [1]

  

如何从IdPenjual中获取值作为参数,以根据IdPenjual显示请求数据?

     

我有这样的Firebase数据库

数据库Firebase

"Request" : {
"1530686546775" : {
  "alamat" : "ff",
  "nama" : "fafa",
  "noHp" : "12345",
  "produk" : [ {
    "diskon" : "0",
    "harga" : "1000",
    "idPenjual" : "08",
    "jumlah" : "1",
    "namaProduk" : "DOUBLE DELIGHT"
  } ],
  "status" : "0",
  "total" : "Rp1.000"
}
  

OrderStatus类

如何从IdPenjual中获取值作为参数,以根据IdPenjual显示请求数据?

public class OrderStatus extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;

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

private String userId;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order_status);

    //Firebase
    database = FirebaseDatabase.getInstance();
    request = database.getReference("Request");

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

    loadOrder(); //mendapatkan data pesanan
}

private void loadOrder() {

    adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(
            Request.class,
            R.layout.order_layout,
            OrderViewHolder.class,
            request

    ) {
        @Override
        protected void populateViewHolder(OrderViewHolder viewHolder, Request model, int position) {

            //mendapatkan status pesanan
            viewHolder.txtOrderId.setText(adapter.getRef(position).getKey());
            viewHolder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
            viewHolder.txtOrderAlamat.setText(model.getAlamat());
            viewHolder.txtOrderNoHP.setText(model.getNoHp());

            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                    //agar tidak eror saat terpencet
                }
            });

        }
    };
    adapter.notifyDataSetChanged();
    recyclerView.setAdapter(adapter);


}

}

这里是班

public class Request {
private String NoHp;
private String Nama;
private String Alamat;
private String Total;
private String Status;
private List<Order> Produk;
private String IdPenjual;

public Request(){

}
public Request(String noHp, String nama, String alamat, String total, List<Order> produk, String idPenjual){
    NoHp = noHp;
    Nama = nama;
    Alamat = alamat;
    Total = total;
    Status = "0";//default 0, 0=Proses, 1=Pengiriman, 2=Terkirim
    Produk = produk;
    IdPenjual = idPenjual;
}


public void setNoHp(String noHp) {
    NoHp = noHp;
}

public String getNoHp() {
    return NoHp;
}

public void setNama(String nama) {
    Nama = nama;
}

public String getNama() {
    return Nama;
}

public String getAlamat() {
    return Alamat;
}

public void setAlamat(String alamat) {
    Alamat = alamat;
}

public void setTotal(String total) {
    Total = total;
}

public String getTotal() {
    return Total;
}

public String getStatus() {
    return Status;
}

public void setStatus(String status) {
    Status = status;
}

public void setProduk(List<Order> produk) { Produk = produk;
}
public List<Order> getProduk() {
    return Produk;
}

public void setIdPenjual(String idPenjual) {
    IdPenjual = idPenjual;
}

public String getIdPenjual() {
    return IdPenjual;
}

}

和ViewHolder

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

    public TextView txtOrderId, txtOrderStatus, txtOrderNoHP, txtOrderAlamat;

    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);
        txtOrderNoHP = (TextView)itemView.findViewById(R.id.order_nohp);
        txtOrderAlamat = (TextView)itemView.findViewById(R.id.order_alamat);


        itemView.setOnClickListener(this);


    }

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

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

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        menu.add(0,0,getAdapterPosition(), "Edit");
        menu.add(0,1,getAdapterPosition(), "Edit");
    }


}

1 个答案:

答案 0 :(得分:0)

 private void checkForidPenjual(String id) {
    Query filterQuery = FirebaseInstance.getFirebaseDatabase().getReference().child("Request").orderByChild("idPenjual").equalTo(email);
    filterQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists())
            {
                // record found
            }
            else {
                // no record found
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

通过这种方式,您将基于参数从Firebase数据库中获取数据。