如何从Firebase获取数据和选择数据

时间:2018-07-13 15:40:38

标签: android firebase firebase-realtime-database

  

Firebase获取特别的数据并显示

我有请求数据,并想显示请求表,当我使用IdSeller 11登录时,请求数据出现时也具有idPenjual 11,而当使用idPerjual 098时,可以显示具有idPenjual 098的请求数据,

您能帮我吗?我已经停留了1周

 "Request" : {
"1531380960725" : {
  "alamat" : "fff",
  "deskripsi" : "dd",
  "nama" : "bintang",
  "noHp" : "098",
  "produk" : [ {
    "diskon" : "90",
    "harga" : "000",
    "idPenjual" : "098",
    "idProduk" : "-LH4DK-bl_8RlK4L5d-u",
    "idToko" : "-LH45THjAkwSE14cllr2",
    "jumlah" : "1",
    "namaProduk" : "Hhh"
  } ],
  "status" : "0"
},
"1531493130828" : {
  "alamat" : "aaa",
  "deskripsi" : "dd",
  "nama" : "bintang",
  "noHp" : "098",
  "produk" : [ {
    "diskon" : "90",
    "harga" : "000",
    "idPenjual" : "098",
    "idProduk" : "-LH4DK-bl_8RlK4L5d-u",
    "idToko" : "-LH45THjAkwSE14cllr2",
    "jumlah" : "1",
    "namaProduk" : "Hhh"
  }, {
    "diskon" : "0",
    "harga" : "90",
    "idPenjual" : "11",
    "idProduk" : "-LH6TwCuUqmzp2idoGRn",
    "idToko" : "-LH3qtCf-QeS2CxSy6Cu",
    "jumlah" : "2",
    "namaProduk" : "ghg"
  } ],
  "status" : "0"
}
  

这是我的课程:)

我有这样的课程

public class OrderStatus extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<Request, OrderViewHolder> adapter;
private ArrayList<String> idTokorequest = new ArrayList<>();
private ArrayAdapter<String> arrayAdapter;
FirebaseDatabase database;
DatabaseReference request, newIdToko;
String idUser="";
Button btnSelect,btnSelect1;

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

    //Firebase
    database = FirebaseDatabase.getInstance();
    request = database.getReference("Request");
    newIdToko = database.getReference("produk");
    recyclerView = (RecyclerView)findViewById(R.id.listOrders);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);


    if (getIntent() !=null)
        idUser = getIntent().getStringExtra("RuserId");
    if (!idUser.isEmpty())
        loadOrder(idUser); //mendapatkan data pesanan

}

private void loadOrder(final String idUser) {
    final DatabaseReference req2 = request;

    //You can use the single or the value.. depending if you want to keep track
    req2.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snap: dataSnapshot.getChildren()) {
                for (DataSnapshot snap2: snap.getChildren()) {
                    for(DataSnapshot snap3: snap2.getChildren()) {
                        for (DataSnapshot snap4: snap3.getChildren()) {
                            if (snap4.getKey().equals("idPenjual") && snap4.getValue().equals(idUser)) {
                                Log.d(snap4.getKey(), snap4.getValue() + "");

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

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

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

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

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

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

@Override
public boolean onContextItemSelected(MenuItem item) {

    if(item.getTitle().equals(Common.UPDATE)){
        showUpdateStatus(adapter.getRef(item.getOrder()).getKey(),adapter.getItem(item.getOrder()));
    }else if(item.getTitle().equals(Common.DELETE)){
        hapus(adapter.getRef(item.getOrder()).getKey());
    }
    return super.onContextItemSelected(item);
}

private void hapus(String key) {
    request.child(key).removeValue();
}

private void showUpdateStatus(String key, final Request item) {

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(OrderStatus.this);
    alertDialog.setTitle("Silahkan Pilih");
    alertDialog.setMessage("Pilih proses");

    LayoutInflater inflater = this.getLayoutInflater();
    View add_menu_layout = inflater.inflate(R.layout.update_order_layout, null);


    btnSelect = add_menu_layout.findViewById(R.id.btnSelect);
    btnSelect1 = add_menu_layout.findViewById(R.id.btnSelect1);

    alertDialog.setView(add_menu_layout);
    alertDialog.setIcon(R.drawable.ic_update_black_24dp);

    //event Button
    btnSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            item.setStatus("1");
        }
    });

    btnSelect1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            item.setStatus("2");
        }
    });

    final String localKey = key;
    //Set Button
    alertDialog.setPositiveButton("YA", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            dialog.dismiss();
            item.getStatus();
            request.child(localKey).setValue(item);

        }
    });
    //Set negative
    alertDialog.setNegativeButton("Batal", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    alertDialog.show();
}

}

0 个答案:

没有答案