Reputation: 938
I have built a note-taking app with Room
, ViewModel
, LiveData
, and RecyclerView
using this tutorial. Everything is working very well. However, when I began to implement the ability to select an image for each note, the RecyclerView
became really laggy and slow as more notes with images were added.
After some research, I discovered that using image loading frameworks like Glide
and Picasso
can make loading faster. So, I tried using Glide to load images from a Uri into the RecyclerView items.
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;
}
}
However, Android Studio gives me the following error - Cannot resolve symbol 'context'
. My question is, what context should I be passing in for this argument? Also, any other suggestions to make the RecyclerView faster when loading images would be greatly appreciated. Thanks!
PS - I am not a professional whatsoever, and only program as a hobby, so I apologize for any misused terms.
Upvotes: 0
Views: 1078
Reputation: 1833
you need to define global variable
Context context ;
and change your code to the following :
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);
}
....
}
Upvotes: 1