首页 > 解决方案 > 在 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);
        }
    }

}

标签: androidfirebase-realtime-databaseandroid-recyclerview

解决方案


在您的category课程中,您应该使用具有图像的 url 并且类型categoryImage应该是String. 然后在您onBindViewHolder使用Glide中加载并显示图像


推荐阅读