错误-尝试在RecyclerView中使用Glide加载图像时出现“无法解析符号'context'

时间:2019-06-13 19:47:01

标签: java android android-recyclerview android-glide listadapter

我使用this tutorial使用RoomViewModelLiveDataRecyclerView构建了一个记笔记应用程序。一切都很好。但是,当我开始实现为每个音符选择图像的功能时,RecyclerView变得很漫长和缓慢,因为添加了更多带图像的音符。

经过一番研究,我发现使用GlidePicasso之类的图像加载框架可以使加载更快。因此,我尝试使用Glide将Uri中的图像加载到RecyclerView项目中。

NoteAdapter.java

public class NoteAdapter extends ListAdapter<Note, NoteAdapter.NoteHolder> {

Context context;

public NoteAdapter() {
    super(DIFF_CALLBACK);
}

private static final DiffUtil.ItemCallback<Note> DIFF_CALLBACK = new DiffUtil.ItemCallback<Note>() {
    @Override
    public boolean areItemsTheSame(@NonNull Note note, @NonNull Note t1) {
        return note.getId() == t1.getId();
    }

    @Override
    public boolean areContentsTheSame(@NonNull Note note, @NonNull Note t1) {
        return note.getTitle().equals(t1.getTitle()) &&  note.getDescription().equals(t1.getDescription()) && note.getImageUri().equals(t1.getImageUri());
    }
};

private OnItemClickListener listener;

@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.note_item, viewGroup, false);
    return new NoteHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull NoteHolder noteHolder, int i) {
    Note currentNote = getItem(i);
    noteHolder.textViewTitle.setText(currentNote.getTitle());
    noteHolder.textViewDescription.setText(currentNote.getDescription());

    Glide.with(context)
            .load(Uri.parse(currentNote.getImageUri()))
            .fit()
            .centerCrop()
            .into(noteHolder.image);

}

public Note getNoteAt(int position) {
    return getItem(position);
}

class NoteHolder extends RecyclerView.ViewHolder {
    private TextView textViewTitle;
    private TextView textViewDescription;
    private ImageView image;

    public NoteHolder(@NonNull View itemView) {
        super(itemView);
        textViewTitle = itemView.findViewById(R.id.text_view_title);
        textViewDescription = itemView.findViewById(R.id.text_view_description);
        image = itemView.findViewById(R.id.image_view);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = getAdapterPosition();

                if (listener != null && position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(getItem(position));
                }
            }
        });
    }
}

public interface OnItemClickListener {
    void onItemClick(Note note);
}

public void setOnItemClickListener(OnItemClickListener listener) {
    this.listener = listener;
}

}

但是,Android Studio给我以下错误-Cannot resolve symbol 'context'。我的问题是,我应该为这个论点传递什么背景?此外,在加载图像时使RecyclerView更快的任何其他建议也将不胜感激。谢谢!

PS-我不是专业人士,只是将其编程为业余爱好,因此对任何误用的条款表示歉意。

1 个答案:

答案 0 :(得分:1)

您需要定义全局变量

 Context context ;

并将您的代码更改为以下内容:

public class NoteAdapter extends ListAdapter<Note, NoteAdapter.NoteHolder> {

....
Context context;
@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    context = viewGroup.getContext();
View itemView = LayoutInflater.from(context).inflate(R.layout.note_item, viewGroup, false);
    return new NoteHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull NoteHolder noteHolder, int i) {
    Note currentNote = getItem(i);
    noteHolder.textViewTitle.setText(currentNote.getTitle());
    noteHolder.textViewDescription.setText(currentNote.getDescription());

    Glide.with(context)
            .load(Uri.parse(currentNote.getImageUri()))
            .fit()
            .centerCrop()
            .into(noteHolder.image);

}

....

}