如何根据价值从先前活动中检索数据

时间:2019-06-21 09:43:58

标签: android firebase firebase-realtime-database

我有一个代码,用于从上一个活动中的值中检索数据,但是会生成重复数据,其中一个包含空数据。

这是我在AdminUsersPayments活动中的代码,用于基于AdminNewOrders活动中的userID显示数据“ Payments”子项。

public class AdminUserPaymentActivity extends AppCompatActivity
{
    private RecyclerView paymentsList;
    RecyclerView.LayoutManager layoutManager;
    private DatabaseReference paymentsRef;

    private String userID = "";


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

        userID = getIntent().getStringExtra("uid");

        paymentsList = findViewById(R.id.payments_list);
        paymentsList.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        paymentsList.setLayoutManager(layoutManager);


        paymentsRef = FirebaseDatabase.getInstance().getReference()
                .child("Cart List").child("Admin View").child(userID);
    }

    @Override
    protected void onStart()
    {
        super.onStart();

        FirebaseRecyclerOptions<Payments> options =
                new FirebaseRecyclerOptions.Builder<Payments>()
                        .setQuery(paymentsRef, Payments.class)
                        .build();


        FirebaseRecyclerAdapter<Payments, PaymentsViewHolder> adapter = new FirebaseRecyclerAdapter<Payments, PaymentsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull PaymentsViewHolder holder, int position, @NonNull Payments model)
            {
                        holder.txtMyBankName.setText("Bank Pengirim: "+ model.getBuyerBank());
                        holder.txtBankAccountName.setText("A/n : "+model.getBuyerAccount());
                        holder.txtBankName.setText("Bank Penerima : "+model.getBank());
                        holder.txtMetods.setText("Dengan Metode : "+model.getMetods());
                        holder.txtTotalTransfer.setText("Rp "+model.getNominal());
                        holder.txtDate.setText("Dibayar Pada Tanggal : "+model.getDate());

            }

            @NonNull
            @Override
            public PaymentsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
            {
               View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.payments_items_layout, parent, false);
               PaymentsViewHolder holder = new PaymentsViewHolder(view);
               return holder;
            }
         };

        paymentsList.setAdapter(adapter);
        adapter.startListening();
    }
}

这是我的AdminNewOrders活动

public class AdminNewOrdersActivity extends AppCompatActivity
{
    private RecyclerView orderList;
    private DatabaseReference ordersRef;


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


        ordersRef = FirebaseDatabase.getInstance().getReference().child("Orders");

        orderList = findViewById(R.id.orders_list);
        orderList.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onStart()
    {
        super.onStart();

        FirebaseRecyclerOptions<AdminOrders> options =
                new FirebaseRecyclerOptions.Builder<AdminOrders>()
                .setQuery(ordersRef, AdminOrders.class)
                .build();

        FirebaseRecyclerAdapter<AdminOrders,AdminOrdersViewHolder> adapter =
                new FirebaseRecyclerAdapter<AdminOrders, AdminOrdersViewHolder>(options) {
                    @Override
                    protected void onBindViewHolder(@NonNull AdminOrdersViewHolder holder, final int position, @NonNull final AdminOrders model)
                    {
                        holder.userName.setText("Nama : " + model.getName());
                        holder.userPhoneNumber.setText("Nomor Telepon : " + model.getPhone());
                        holder.userTotalprice.setText("Total Harga = Rp " + model.getTotalAmount());
                        holder.userDateTime.setText("Dipesan Pada: " + model.getDate() + " " + model.getTime());
                        holder.usershippingAddress.setText("Alamat: " + model.getAddress() + ", " + model.getCity());
                        holder.userStatePayments.setText("Status: "+ model.getState());

                        holder.showOrdersBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view)
                            {
                                String uID = getRef(position).getKey();

                                Intent intent = new Intent(AdminNewOrdersActivity.this, AdminUserProductsActivity.class);
                                intent.putExtra("uid", uID);
                                startActivity(intent);
                            }
                        });

                        holder.checkPayments.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view)
                            {
                                String uID = getRef(position).getKey();

                                Intent intent = new Intent(AdminNewOrdersActivity.this, AdminUserPaymentActivity.class);
                                intent.putExtra("uid", uID);
                                startActivity(intent);
                            }
                        });


                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view)
                            {
                               CharSequence options[] = new CharSequence[]
                                       {
                                               "Ya",
                                               "Tidak"
                                       };

                                AlertDialog.Builder builder = new AlertDialog.Builder(AdminNewOrdersActivity.this);
                                        builder.setTitle("Sudah Mengirim Barang ini ?");

                                        builder.setItems(options, new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i)
                                            {
                                               if (i == 0)
                                               {
                                                   String uID = getRef(position).getKey();


                                                   RemoverOrder(uID);
                                               }
                                               else
                                               {
                                                   finish();
                                               }
                                            }
                                        });
                                        builder.show();
                            }
                        });
                    }

                    @NonNull
                    @Override
                    public AdminOrdersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
                    {
                        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.orders_admin_layout, parent, false);
                        return new AdminOrdersViewHolder(view);
                    }
                };
        orderList.setAdapter(adapter);
        adapter.startListening();
    }




    public static class AdminOrdersViewHolder extends RecyclerView.ViewHolder
    {
        public TextView userName, userPhoneNumber, userTotalprice, userDateTime, usershippingAddress, userStatePayments;
        public Button showOrdersBtn, checkPayments;
        public AdminOrdersViewHolder(View itemView)

        {
            super(itemView);


            userName = itemView.findViewById(R.id.orders_user_name);
            userPhoneNumber = itemView.findViewById(R.id.orders_phone_number);
            userTotalprice = itemView.findViewById(R.id.orders_total_price);
            userDateTime = itemView.findViewById(R.id.orders_date_time);
            usershippingAddress = itemView.findViewById(R.id.orders_address_city);
            userStatePayments = itemView.findViewById(R.id.orders_state);
            showOrdersBtn = itemView.findViewById(R.id.show_all_products);
            checkPayments = itemView.findViewById(R.id.show_payment_status);
        }
    }




    private void RemoverOrder(String uID)
    {
        ordersRef.child(uID).removeValue();
    }
}

这是我的Payments ViewHolder

public class PaymentsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
    public TextView txtBankName, txtMyBankName, txtBankAccountName, txtTotalTransfer, txtMetods, txtDate;
    private ItemClickListener itemClickListener;


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

        txtBankName = itemView.findViewById(R.id.payments_bank_destination);
        txtMyBankName = itemView.findViewById(R.id.payments_bank_name);
        txtBankAccountName = itemView.findViewById(R.id.payments_bank_account);
        txtTotalTransfer = itemView.findViewById(R.id.payments_total_transfer);
        txtMetods = itemView.findViewById(R.id.payments_bank_metods);
        txtDate = itemView.findViewById(R.id.payments_date);

    }

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

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

付款模式类

public class Payments
{
    private String bank, buyerBank, buyerAccount, nominal, metods, date, time;

    public Payments()
    {

    }

    public Payments(String bank, String buyerBank, String buyerAccount, String nominal, String metods, String date, String time)
    {
        this.bank = bank;
        this.buyerBank = buyerBank;
        this.buyerAccount = buyerAccount;
        this.nominal = nominal;
        this.metods = metods;
        this.date = date;
        this.time = time;
    }

    public String getBank() {
        return bank;
    }

    public void setBank(String bank) {
        this.bank = bank;
    }

    public String getBuyerBank() {
        return buyerBank;
    }

    public void setBuyerBank(String buyerBank) {
        this.buyerBank = buyerBank;
    }

    public String getBuyerAccount() {
        return buyerAccount;
    }

    public void setBuyerAccount(String buyerAccount) {
        this.buyerAccount = buyerAccount;
    }

    public String getNominal() {
        return nominal;
    }

    public void setNominal(String nominal) {
        this.nominal = nominal;
    }

    public String getMetods() {
        return metods;
    }

    public void setMetods(String metods) {
        this.metods = metods;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

我的结构化数据库 my firebase Structured

我的输出中包含重复数据且值为空

my output

我希望输出可以显示基于AdminNewOrders活动中的userID的“付款”数据,而没有具有空值的重复数据

0 个答案:

没有答案