簡體   English   中英

如何從 firebase 數據庫中檢索 object?

[英]How to retrieve an object from firebase database?

我正在嘗試從數據庫中的 ArrayList 中檢索 object,但是當我檢索它時,其中一個 object 屬性返回一個空字符串,而不是保存在數據庫中的字符串,但是,其他屬性正在返回它們的值。

數據庫圖像:

我的數據庫

getAmount 從數據庫返回匹配的字符串,但getFoodName 返回一個空字符串而不是 "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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM