i try to set data from realtime database to recyclerview but it's returning null at Textview and ImageView, my data have text and url from storage, there my code:
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);
}
}
my 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());
}
});
}`
example for my json structure:
"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"
}
My Realtime Database rules are set to true to both read and write. I've tried everything that I can from the internet but nothing seem to have an effect. How I can resolve this problem. Any help I can get I appreciate it really much.