简体   繁体   English

Firebase 数据库提供空值

[英]Firebase Database gives null values

This is my Firebase Database snippet这是我的 Firebase 数据库片段

数据库片段

I am trying to get the name of the expense and the expense amount and set it into a RecyclerView .我正在尝试获取费用的名称和费用金额并将其设置为RecyclerView

This is my adapter for the RecyclerView:这是我的 RecyclerView 适配器:

public class ExpenseAdapter extends RecyclerView.Adapter<ExpenseAdapter.MyViewHolder> {

    Context context;
    ArrayList<Expense> list;

    public ExpenseAdapter(Context context, ArrayList<Expense> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.expense_item, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ExpenseAdapter.MyViewHolder holder, int position) {
        Expense expense = list.get(position);
        holder.expenseNameText.setText(expense.getName());
        holder.expenseAmountText.setText(expense.getAmount());
        holder.expenseAmountSign.setText(expense.getType());
//        String type = expense.getType();
//        if(type.equals("Credit")) {
//            holder.expenseAmountSign.setText("+ ");
//            holder.expenseAmountSign.setTextColor(Color.GREEN);
//        } else if(type.equals("Debit")) {
//            holder.expenseAmountSign.setText("- ");
//            holder.expenseAmountSign.setTextColor(Color.RED);
//        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView expenseNameText, expenseAmountText, expenseAmountSign;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            expenseNameText = itemView.findViewById(R.id.expenseNameText);
            expenseAmountText = itemView.findViewById(R.id.expenseAmountText);
            expenseAmountSign = itemView.findViewById(R.id.expenseAmountSign);

        }
    }

}

This is my POJO class for Expense Model这是我的费用模型 POJO 类

package com.inamul.ledgers;

public class Expense {

    String name, amount, type;

    public Expense() {  }

    public Expense(String name, String amount, String type) {
        this.name = name;
        this.amount = amount;
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Expense{" +
                "name='" + name + '\'' +
                ", amount='" + amount + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}

This is my code snippet这是我的代码片段

String path = Objects.requireNonNull(mAuth.getUid()) + "/ledgers/" + ledgerName + "/expenses/";
// Database Reference
databaseReference = FirebaseDatabase.getInstance().getReference(path);
databaseReference.addValueEventListener(new ValueEventListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                list.clear();
                for(DataSnapshot dataSnapshot: snapshot.getChildren()) {
                    Expense expense = dataSnapshot.getValue(Expense.class);
                    assert expense != null;
                    Log.d("TAG", expense.toString());
                    list.add(expense);
                }
                expenseAdapter.notifyDataSetChanged();
            }

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

            }
});

The log shows that every value in the expense class has been set to null and the database path seems to be fine日志显示费用类中的每个值都已设置为空,并且数据库路径似乎没问题

2022-07-19 13:05:01.761 26621-26621/com.inamul.expenser D/TAG: Expense{name='null', amount='null', type='null'}
2022-07-19 13:05:01.755 26621-26621/com.inamul.expenser D/PATH: XELPjRzISJN969d0ttPQeheyG1r1/ledgers/Company/expenses/

This is the output这是输出

在此处输入图像描述

What am I doing wrong?我究竟做错了什么?

You're getting null for each property:每个属性都为 null:

D/TAG: Expense{name='null', amount='null', type='null'} D/TAG: 费用{name='null', amount='null', type='null'}

Because in the database there is no property called name , amount , or type .因为在数据库中没有名为nameamounttype的属性。 So the simplest solution I can think of would be to change the Expense class declaration to:所以我能想到的最简单的解决方案是将Expense类声明更改为:

public class Expense {
    String expenseName, expenseAmount, expenseType;

    public Expense() {  }

    public Expense(String expenseName, String expenseAmount, String expenseType) {
        this.expenseName = expenseName;
        this.expenseAmount = expenseAmount;
        this.expenseType = expenseType;
    }

    public String getExpenseName() {
        return expenseName;
    }

    public void setExpenseName(String expenseName) {
        this.expenseName = expenseName;
    }

    public String getExpenseAmount() {
        return expenseAmount;
    }

    public void setExpenseAmount(String expenseAmount) {
        this.expenseAmount = expenseAmount;
    }

    public String getExpenseType() {
        return expenseType;
    }

    public void setExpenseType(String expenseType) {
        this.expenseType = expenseType;
    }

    @Override
    public String toString() {
        return "Expense{" +
                "expenseName='" + expenseName + '\'' +
                ", expenseAmount='" + expenseAmount + '\'' +
                ", expenseType='" + expenseType + '\'' +
                '}';
    }
}

So it can match the name in the database.所以它可以匹配数据库中的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM