【发布时间】:2021-09-12 21:27:00
【问题描述】:
我有一个可与此适配器一起使用的回收站视图。在数据库中,我有一个键、一个名称和一个转换为照片的图像。但是如果我重新启动程序,图片不会显示在列表中,如果我在数据库中添加新数据,图片就会停止显示在列表中。
公共类适配器扩展 RecyclerView.Adapter
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);
}
}
}
【问题讨论】:
-
您是否尝试过使用 glide 来代替使用 uri 加载图像?
标签: android database android-room adapter