首页 > 解决方案 > 如何让图片出现在列表中

问题描述

我有一个适用于此适配器的回收站视图。在数据库中,我有一个键、一个名称和一个转换为照片的图像。但是如果我重新启动程序,图片不会显示在列表中,如果我向数据库添加新数据,图片就会停止显示在列表中。

公共类适配器扩展 RecyclerView.Adapter<Adapter.NoteViewHolder> {

private SortedList<Note> sortedList;

public Adapter() {
    sortedList = new SortedList<>(Note.class, new SortedList.Callback<Note>() {
        @Override
        public int compare(Note o1, Note o2) {

            return 0;
        }

        @Override
        public void onChanged(int position, int count) {
            notifyItemRangeChanged(position, count);
        }

        @Override
        public boolean areContentsTheSame(Note oldItem, Note newItem) {
            return oldItem.equals(newItem);
        }

        @Override
        public boolean areItemsTheSame(Note item1, Note item2) {
            return item1.uid == item2.uid;
        }

        @Override
        public void onInserted(int position, int count) {
            notifyItemRangeInserted(position, count);
        }

        @Override
        public void onRemoved(int position, int count) {
            notifyItemRangeRemoved(position, count);
        }

        @Override
        public void onMoved(int fromPosition, int toPosition) {
            notifyItemMoved(fromPosition, toPosition);
        }
    });
}

@NonNull
@NotNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
    return new NoteViewHolder(
            LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false)
    );
}

@Override
public void onBindViewHolder(@NonNull @NotNull NoteViewHolder holder, int position) {
    holder.bind(sortedList.get(position));
}

@Override
public int getItemCount() {
    return sortedList.size();
}

public void setItems(List<Note> notes) {
    sortedList.replaceAll(notes);
}

static class NoteViewHolder extends RecyclerView.ViewHolder{

    TextView textView;
    ImageView imageView;
    Uri uri;

    Note note;

    public NoteViewHolder (@NonNull View itemView) {
        super (itemView);

        textView = itemView.findViewById(R.id.name);
        imageView = itemView.findViewById(R.id.icon);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CreateActivity.start((Activity) itemView.getContext(), note);
            }
        });
    }

    public void bind (Note note) {
        this.note = note;

        uri = Uri.parse(note.img);
        textView.setText(note.text);
        imageView.setImageURI(uri);
    }
}

}

标签: androiddatabaseandroid-roomadapter

解决方案


尝试改用 Glide,将 Glide 包含在您的项目中,将此行添加到依赖项部分的 build.Gradle(Module App) 中:

dependencies {
...
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
...
}

然后尝试使用这行代码而不是加载图像 imageView.setImageURI(uri);

   Glide.with(itemView.getContext()).load(uri).into(imageView);

你需要一个上下文对象,所以如果你可以在绑定方法中传递一个片段/活动上下文(或者任何其他上下文或片段或活动对象都可以完成这项工作。)。

例如,您可以将itemView.getContext()上下文或 itemView 本身存储为全局变量,并在需要时在 bind 方法中使用它。


推荐阅读