android - 在 onBindViewHolder 中为 RecyclerView 设置 Firebase 实时数据库参考中的图像
问题描述
我正在尝试在我的设备上设置一个 RecyclerView,HomeFragment
而我正处于设置适配器的位置。如何在我的onBindViewHolder
函数中设置图像?我需要Glide.with()
在我的onBindViewHolder
函数中更改类吗?另外,对于DatabaseReference
,我如何获取实际的 uid 以供参考?谢谢!
我的数据库参考之一
类别.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>
类别.java
public class category {
private String categoryImage;
private URL categoryImageUrl;
private String category;
private String uid;
public category() {}
public String getCategoryImage() {
return categoryImage;
}
public void setCategoryImage(String categoryImage) {
this.categoryImage = categoryImage;
}
public URL getCategoryImageUrl() { return categoryImageUrl; }
public void setCategoryImageUrl(URL categoryImageUrl) { this.categoryImageUrl = categoryImageUrl; }
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());
DatabaseReference url_db = FirebaseDatabase.getInstance().getReference().child("job-category").child(model.getUid()).child("categoryImage");
url_db.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String image = snapshot.child("categoryImage").getValue(String.class);
Glide.with(this, category.class).load(image).into(holder.categoryImage);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@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);
}
}
}
解决方案
在您的category
课程中,您应该使用具有图像的 url 并且类型categoryImage
应该是String
. 然后在您onBindViewHolder
使用Glide中加载并显示图像
推荐阅读
- c# - 如何在 WPF 中存在于父用户控件的列表中显示子用户控件
- python - Python 阅读电子邮件信息
- python - 如何根据重新绑定变量的条件对以下代码进行重复数据删除?
- postgresql - 追加到现有文件而不被 psql 命令覆盖 \o
- bash - 安装示例、二进制文件和 Docker 映像在 mac 上不起作用
- php - 使用 PHP 获取 Ledger Report Mysql 表
- python - 如何在python方法调用上创建循环
- php - 在 cookie 中包含 httponly
- c# - C#桌面图表绘制完成需要很长时间
- javascript - 实现nodejs es 6时遇到错误