RecyclerView读取Firebase数据时TextView未更新问题
问题定位
你的代码存在4处直接导致数据无法正常显示的问题:
ProductAdapter的getItemCount()方法写死返回0,RecyclerView会判定列表无数据,完全不会执行条目绑定逻辑,界面一直保留布局预置的默认文本。onBindViewHolder()方法中控件赋值逻辑错误:遗漏了name控件的赋值,还错误地先把商品名赋值给了描述控件,后续又被描述值覆盖,商品名永远无法显示。ProductModel缺少无参构造方法,Firebase Realtime Database反序列化数据时要求模型类必须提供无参构造,否则会出现数据映射失败。- Firebase数据回调中没有清空旧列表:
addValueEventListener每次触发都会返回节点下全量数据,直接往列表add会导致数据更新时重复添加条目。 - 额外布局问题:列表项布局
tab_one.xml的根布局高度设置为match_parent,会导致单个列表项占满整屏,一屏只能看到一个商品。
修正代码
1. 修正ProductModel,添加无参构造
public class ProductModel { private String name; private String price; private String description; // Firebase反序列化必需的无参构造 public ProductModel() {} public void setName(String name) { this.name = name; } public void setPrice(String price) { this.price = price; } public void setDescription(String description) { this.description = description; } public String getName() { return name; } public String getPrice() { return price; } public String getDescription() { return description; } }
2. 修正ProductAdapter逻辑
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> { Context context; ArrayList<ProductModel> list; public ProductAdapter(Context context, ArrayList<ProductModel> list){ this.context = context; this.list = list; } @NonNull @Override public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(context).inflate(R.layout.tab_one,parent,false); return new ProductAdapter.ProductViewHolder(v); } @Override public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) { ProductModel productModel = list.get(position); // 修正控件赋值对应关系 holder.name.setText(productModel.getName()); holder.price.setText(productModel.getPrice()); holder.description.setText(productModel.getDescription()); } @Override public int getItemCount() { // 返回列表实际长度,禁止写死0 return list.size(); } public static class ProductViewHolder extends RecyclerView.ViewHolder{ TextView name, description, price; public ProductViewHolder(@NonNull View itemView) { super(itemView); name = itemView.findViewById(R.id.productName); description = itemView.findViewById(R.id.desc); price = itemView.findViewById(R.id.price); } } }
3. 修正ProductList中Firebase回调逻辑
public class ProductList extends AppCompatActivity { RecyclerView recyclerView; ProductAdapter adapter; ArrayList<ProductModel> list; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.product_list); DatabaseReference ref = FirebaseDatabase.getInstance().getReference("products"); recyclerView = findViewById(R.id.productList); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); list = new ArrayList<>(); adapter = new ProductAdapter(this,list); recyclerView.setAdapter(adapter); ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { // 拉取全量数据前先清空旧列表,避免重复数据 list.clear(); for (DataSnapshot dataSnapshot : snapshot.getChildren()) { ProductModel product = dataSnapshot.getValue(ProductModel.class); list.add(product); } adapter.notifyDataSetChanged(); } @Override public void onCancelled(@NonNull DatabaseError error) { // 建议在此处打印错误日志,方便后续排查权限、网络类异常 } }); } }
4. 修正列表项布局高度
打开tab_one.xml,把根FrameLayout的android:layout_height="match_parent"改为android:layout_height="wrap_content",避免单条目占满整屏。
改完以上内容重新运行即可正常加载Firebase中的商品数据。
内容的提问来源于stack exchange,提问作者dun
相关产品推荐
相关产品推荐

