com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.pasarkaget.fajar.pasarkaget.Model.Payments

时间:2019-06-17 19:27:42

标签: java android firebase firebase-realtime-database

我在检索付款数据时遇到问题。总是说我不能将对象类型转换为:

  
    

com.google.firebase.database.DatabaseException:无法转换对象     类型为java.lang.String的类型     com.pasarkaget.fajar.pasarkaget.Model.Payments

  

我认为我的模型付款中存在此错误,但我不知道我的错误。有人可以帮助我吗?

这是我的AdminUserPaymentsActivity,我必须在其中显示某些用户的付款数据。

 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).child("Payments");
            }

            @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(model.getBuyerBank());
                                holder.txtBankAccountName.setText(model.getBuyerAccount());
                                holder.txtBankName.setText(model.getBank());
                                holder.txtMetods.setText(model.getMetods());
                                holder.txtTotalTransfer.setText("Rp " + model.getNominal());
                                holder.txtState.setText(model.getState());
                                holder.txtDate.setText("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();
            }
        }

我的Viewholder类

          public class PaymentsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
            {
                public TextView txtBankName, txtMyBankName, txtBankAccountName, txtTotalTransfer, txtMetods, txtState, 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);
                    txtState = itemView.findViewById(R.id.payments_state);
                    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;
                }
            }

    And Lastly, My Model Payments class



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

            public Payments()
            {

            }

            public Payments(String bank, String buyerBank, String buyerAccount, String nominal, String metods, String state, String date, String time)
            {
                this.bank = bank;
                this.buyerBank = buyerBank;
                this.buyerAccount = buyerAccount;
                this.nominal = nominal;
                this.metods = metods;
                this.state= state;
                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 getState() {
                return state;
            }

            public void setState(String state) {
                this.state = state;
            }

            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;
                }
            }

我的Firebase数据库结构如下:

my structured firebase

1 个答案:

答案 0 :(得分:0)

A<U>::MemberType<T2>对象传递给paymentsRef对象时,这意味着您正在寻找FirebaseRecyclerOptions<Payments>对象,但是在引用中,只有String和类型的对象不是 Payments,这就是为什么会出现该错误的原因。要解决此问题,只需删除对Payments的调用:

.child("Payments")

您将能够获取paymentsRef = FirebaseDatabase.getInstance().getReference() .child("Cart List").child("Admin View").child(userID); 节点内存在的所有Payments对象。