首页 > 解决方案 > 无法使用 Firebase UI Recycler Adapter 在 Recycler 视图中加载内容

问题描述

我一直在尝试从 Firebase 实时数据库中填充我的 Recycler 视图,但结果如下: Empty Cards in Recycler View

我很确定我已遵循所有步骤。令人惊讶的是,即使内容没有加载,已加载的空卡数量也是正确的。

这是我的数据库: Firebase 数据库的图片

这是我的带有卡片视图的布局文件:

<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginStart="15dp"
    android:layout_marginEnd="15dp"
    app:cardElevation="15dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">

        <ImageView
            android:id="@+id/food_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:srcCompat="@tools:sample/avatars" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="10dp">

            <TextView
                android:id="@+id/food_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ITEM NAME"
                android:textSize="18sp" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:padding="10dp">
                <TextView
                    android:id="@+id/food_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="PRICE:"/>
            </LinearLayout>

            <com.google.android.material.button.MaterialButton
                android:id="@+id/addtocart_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                style="?attr/borderlessButtonStyle"
                android:text="ADD TO CART"/>

        </LinearLayout>

    </LinearLayout>



</com.google.android.material.card.MaterialCardView>

这是我的模型类:

package com.example.dubstep.Model;

public class FoodItem {
    String base_price;
    String name;

    public FoodItem(){

    }

    public FoodItem(String price, String name) {
        this.base_price = price;
        this.name = name;
    }

    public String getBase_price() {
        return base_price;
    }

    public void setBase_price(String base_price) {
        this.base_price = base_price;
    }

    public String getName() {
        return name;
    }

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

这是我的 ViewHolder 类:

public class FoodItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView mFoodItemName;
    public ImageView mFoodImage;
    public TextView mFoodItemPrice;
    public MaterialButton mAddToCart;

    public ItemClickListener listener;

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

        mFoodItemName = (TextView) itemView.findViewById(R.id.food_name);
        mFoodItemPrice = (TextView) itemView.findViewById(R.id.food_price);
        mFoodImage = (ImageView) itemView.findViewById(R.id.food_image);
        mAddToCart = (MaterialButton) itemView.findViewById(R.id.addtocart_btn);
    }

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

    @Override
    public void onClick(View v) {
        listener.onClick(v,getAdapterPosition(),false);
    }
}

我在主活动 OnStart 中使用了 Firebase Recycler Adapter:

FirebaseRecyclerOptions<FoodItem> options = new FirebaseRecyclerOptions.Builder<FoodItem>().setQuery(foodref,FoodItem.class).build();
        FirebaseRecyclerAdapter<FoodItem, FoodItemViewHolder> adapter =
                new FirebaseRecyclerAdapter<FoodItem, FoodItemViewHolder>(options) {
                    @Override
                    protected void onBindViewHolder(@NonNull FoodItemViewHolder holder, int position, @NonNull FoodItem model) {

                        holder.mFoodItemName.setText(model.getName());
                        holder.mFoodItemPrice.setText("Price: "+model.getBase_price());

                    }

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

                        return holder;
                    }
                };

        recyclerView.setAdapter(adapter);
        adapter.startListening();

在我的 OnCreate 中,我已经初始化了我的 Recycler 视图和布局管理器:

foodref = FirebaseDatabase.getInstance().getReference().child("food_menu");

recyclerView = findViewById(R.id.main_recyclerview);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);

有人可以帮助我并告诉我哪里可能出错了吗?

谢谢!

标签: androidfirebasefirebase-realtime-databaseandroid-recyclerviewfirebaseui

解决方案


推荐阅读