Android Firebase实时数据库数据在RecyclerView中返回空值问题
Firebase实时数据库数据加载到RecyclerView为空的问题
尝试将Firebase实时数据库中的数据加载到RecyclerView,但TextView和ImageView显示空值。数据包含文本内容和云存储的图片URL,Firebase实时数据库规则已设置为允许读写,尝试多种方法无效,求解决。
Adapter代码
@Override public void onBindViewHolder(@NonNull PlantsViewHolder holder, int position) { PlantCategory plantCategory = plantCategoryList.get(position); if(plantCategory == null){ return; }else { Glide.with(context) .load(plantCategory.getCategoryImageURL()) .placeholder(R.drawable.img_hard_level1) // Hình ảnh placeholder tùy chọn .error(R.drawable.img_error_icon) .into(holder.imgPlantCategory); holder.tvCategoryName.setText(""+plantCategory.getCategoryName()); } } public class PlantsViewHolder extends RecyclerView.ViewHolder{ private ImageView imgPlantCategory; private TextView tvCategoryName; public PlantsViewHolder(@NonNull View itemView) { super(itemView); imgPlantCategory = itemView.findViewById(R.id.img_category); tvCategoryName = itemView.findViewById(R.id.tv_plant_category); } }
Fragment代码
private List<PlantCategory> plantCategoryList; private void setPlantCategoryAdapter(){ recyclerView = mView.findViewById(R.id.rcv_cate); plantCategoryAdapter = new PlantCategoryAdapter(getContext(),R.layout.item_category_search, this, R.id.rcv_cate); GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2); recyclerView.setLayoutManager(gridLayoutManager); plantCategoryList = new ArrayList<>(); plantCategoryAdapter.setData(plantCategoryList); recyclerView.setAdapter(plantCategoryAdapter); getListCateGory(); } private void getListCateGory(){ DatabaseReference categoriesRef = FirebaseDatabase.getInstance().getReference().child("Category"); categoriesRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for( DataSnapshot snapshot : dataSnapshot.getChildren()){ PlantCategory category = snapshot.getValue(PlantCategory.class); plantCategoryList.add(category); } plantCategoryAdapter.notifyDataSetChanged(); } @Override public void onCancelled(@NonNull DatabaseError error) { Log.e(TAG, "hhh" + error.getMessage()); } }); }
数据库JSON结构示例
"Category": { "1": { "CategoryID": 1, "CategoryName": "Dây leo", "CategoryImage": "https://firebasestorage.googleapis.com/v0/b/plant-box-b4958.appspot.com/o/Category%20Image%2FClimber.jpg?alt=media&token=e9b203e9-7bdf-478d-8575-9b2534ed7067" }, "2": { "CategoryID": 2, "CategoryName": "Cây lá kim", "CategoryImage": "https://firebasestorage.googleapis.com/v0/b/plant-box-b4958.appspot.com/o/Category%20Image%2FConifers.jpg?alt=media&token=4ccd001b-dcb7-47d2-8b4c-f49406e528d1" } }
问题排查与解决方案
1. 修正实体类字段映射
数据库中图片字段名为CategoryImage,但你在Adapter中调用的是getCategoryImageURL(),字段名不匹配导致无法正确赋值。修改PlantCategory实体类,确保字段名和数据库键一致,或用@PropertyName注解指定映射:
public class PlantCategory { private int CategoryID; private String CategoryName; private String CategoryImage; // Firebase序列化必须有无参构造函数 public PlantCategory() {} // Getter和Setter方法 public int getCategoryID() { return CategoryID; } public void setCategoryID(int categoryID) { CategoryID = categoryID; } public String getCategoryName() { return CategoryName; } public void setCategoryName(String categoryName) { CategoryName = categoryName; } public String getCategoryImage() { return CategoryImage; } public void setCategoryImage(String categoryImage) { CategoryImage = categoryImage; } }
同时修改Adapter中的加载代码:
Glide.with(context) .load(plantCategory.getCategoryImage()) .placeholder(R.drawable.img_hard_level1) .error(R.drawable.img_error_icon) .into(holder.imgPlantCategory);
2. 避免重复数据与空对象
每次onDataChange触发时,先清空列表再添加新数据,同时对获取的对象判空:
@Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { plantCategoryList.clear(); for( DataSnapshot snapshot : dataSnapshot.getChildren()){ PlantCategory category = snapshot.getValue(PlantCategory.class); if(category != null) { plantCategoryList.add(category); } } plantCategoryAdapter.notifyDataSetChanged(); }
3. 验证数据获取状态
在onDataChange中添加日志,确认是否成功从数据库拿到数据:
Log.d(TAG, "共获取到" + dataSnapshot.getChildrenCount() + "条分类数据"); for( DataSnapshot snapshot : dataSnapshot.getChildren()){ PlantCategory category = snapshot.getValue(PlantCategory.class); Log.d(TAG, "分类名称:" + (category != null ? category.getCategoryName() : "空对象")); plantCategoryList.add(category); }
如果日志显示数据为空,检查数据库路径是否正确(注意节点名大小写是否和数据库一致)。
4. 检查Glide依赖与网络权限
确保项目中添加了Glide依赖,并且在AndroidManifest.xml中声明网络权限:
<uses-permission android:name="android.permission.INTERNET" />
内容的提问来源于stack exchange,提问作者xilhwrd AQR
相关产品推荐
相关产品推荐

