如何从 firebase 数据库中检索对象?

How to retrieve an object from firebase database?

我试图从数据库中的 ArrayList 中检索一个对象,但是当我检索它时,对象属性之一 returns 是一个空字符串,而不是数据库中保存的字符串,但是,其他属性正在返回它们的值。

数据库图像:

getAmount returns 来自数据库的匹配字符串但是 getFoodName returns 一个空字符串而不是“egg”.

 reference.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                User userProfile=snapshot.getValue(User.class);
                if(userProfile!=null)
                {

                    if(userProfile.morning_List!=null)
                    {
                        for(int i=0;i<userProfile.morning_List.size();i++)
                        {
                            FoodItem foodItem=new FoodItem(userProfile.morning_List.get(i));
                            mFoodList.add(new FoodItem(foodItem.getFoodName(),foodItem.getAmount()));
                        }
                      
                }

食品项目class

public class FoodItem {
    private String fdName;
    private String amount;
  
    public FoodItem(String foodName, String foodAmount) {
        this.fdName = foodName;
        this.amount=foodAmount;
    }
   

    public String getFoodName() {
        return this.fdName;
    }

    public String getAmount() {
        return this.amount;
    }
}

列出适配器 class:

public class FoodListAdapter extends RecyclerView.Adapter<FoodListAdapter.FoodViewHolder>{
    private ArrayList<FoodItem> mFoodList;
         
    public static class FoodViewHolder extends RecyclerView.ViewHolder{
        public TextView mFoodName;
        public TextView mAmount;
        public FoodViewHolder(@NonNull View itemView,final OnItemClickListener listener) {
            super(itemView);
            mFoodName = itemView.findViewById(R.id.fdName);
            mAmount = itemView.findViewById(R.id.amount);
         }
    }

    public FoodListAdapter(ArrayList<FoodItem> foodList) {
        mFoodList = foodList;
    }

    @NonNull
    @Override
    public FoodListAdapter.FoodViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_item, parent, false);
        FoodViewHolder evh = new FoodViewHolder(v, mListener);
        return evh;

    }

    @Override
    public void onBindViewHolder(@NonNull FoodListAdapter.FoodViewHolder holder, int position) {
        FoodItem currentItem = mFoodList.get(position);
        holder.mFoodName.setText(currentItem.getFoodName());
        holder.mAmount.setText(currentItem.getAmount());
    }
    @Override
    public int getItemCount() {
        return mFoodList.size();      
    }
}

我认为这可能是因为您的字段名为 fdName,而数据库中的 属性 名为 foodName

Firebase 使用 getter 和 setter 来确定 属性 的名称,或者如果缺少这些,则使用字段的名称。所以它正在数据库中寻找一个名为 fdName 的 属性。

解决方案是重命名您的字段以匹配数据库中的 属性 名称:

public class FoodItem {
    private String foodName; // 
    private String amount;
  
    public FoodItem(String foodName, String foodAmount) {
        this.foodName = foodName;
        this.amount=foodAmount;
    }
   

    public String getFoodName() {
        return this.foodName; // 
    }

    public String getAmount() {
        return this.amount;
    }
}