I'm trying to set up a RecyclerView on my HomeFragment
and I'm at the point where I'm setting up the adapter. How do I set the image in my onBindViewHolder
function? Do I need to refactor my category.java
class to be able to actually set the image? Thank you!
category.xml
<androidx.cardview.widget.CardView 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="match_parent"
android:background="@drawable/category_view_background">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@ id/categoryImageView"
android:layout_width="420dp"
android:layout_height="676dp"
android:background="@drawable/image_rounded_top_corners"
android:scaleType="fitXY"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/profile_photo_"
android:contentDescription="@string/category_image" />
<TextView
android:id="@ id/categoryNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/category_name"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/categoryImageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
category.java
public class category {
private Image categoryImage;
private String category;
private String uid;
public category() {}
public Image getCategoryImage() {
return categoryImage;
}
public void setCategoryImage(Image categoryImage) {
this.categoryImage = categoryImage;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String setCategory() {
return "";
}
}
categoryAdapter.java
public class categoryAdapter extends FirebaseRecyclerAdapter<category, categoryAdapter.categoryViewholder>{
public categoryAdapter(@NonNull FirebaseRecyclerOptions<category> options) {
super(options);
}
@Override protected void onBindViewHolder(@NonNull categoryAdapter.categoryViewholder holder, int position, @NonNull category model) {
holder.category.setText(model.setCategory());
}
@NonNull @Override public categoryViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category, parent, false);
return new categoryViewholder(view);
}
static class categoryViewholder extends RecyclerView.ViewHolder {
TextView category;
Image categoryImage;
public categoryViewholder(@NonNull View itemView) {
super(itemView);
category = itemView.findViewById(R.id.categoryNameTextView);
categoryImage = itemView.findViewById(R.id.categoryImageView);
}
}
}
CodePudding user response:
In your category
class you should use have url of image and the type of categoryImage
should be String
.
then to load and show image in your onBindViewHolder
use Glide