android - 无法使用 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);
有人可以帮助我并告诉我哪里可能出错了吗?
谢谢!
解决方案
推荐阅读
- c# - 将多维数组属性公开的最佳方式?
- email - 通过“mailx”发送电子邮件不适用于某些电子邮件域
- android - (x) 分钟后滑动刷新布局重试进度条
- r - 理解 R 中的 time() 和 cycle() 函数
- entity-framework - 在另一个实体中引用两次的同一实体正在创建问题,同时将父母与孩子一起保存
- python - Python:计算值并发送电子邮件
- hive - Tableau 10.3 和 Cloudera Hive 2.0 之间的兼容性问题
- react-native - 无法接收转储组件的道具
- java - org.apache.axis2.AxisFault:必须了解标头 http://www.ebxml.org/namespaces/messageHeader 检查失败
- html - angular 6 ngx-treeview 自定义样式