Я не могу понять, почему я не смог загрузить какое-либо изображение (я пробовал несколько URL-адресов). Я пытался использовать класс AsyncTask, и проблема до сих пор не решена. Мой класс адаптера указан ниже, любая помощь будет принята с благодарностью.
Я пытаюсь загрузить изображение (в этом тестовом примере одно и то же изображение) для каждой записи RecyclerView. Представление по умолчанию (оранжевый квадрат) появляется, когда я не пытаюсь установить изображение, полученное из URL, но если я пытаюсь это сделать, ImageView просто остается пустым.
EntryAdapter
public class EntryAdapter extends RecyclerView.Adapter<EntryAdapter.EntryViewHolder> {
private Entry[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class EntryViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public String source;
public TextView textContent, title, label, author;
public ImageView thumbnail;
public CardView topCard, mainCard;
public EntryViewHolder(View v) {
super(v);
title = v.findViewById(R.id.title);
textContent = v.findViewById(R.id.textContent);
topCard = v.findViewById(R.id.top_card);
mainCard = v.findViewById(R.id.main_card);
thumbnail = mainCard.findViewById(R.id.thumbnail);
author = mainCard.findViewById(R.id.author);
label = topCard.findViewById(R.id.label);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public EntryAdapter(Entry[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public EntryAdapter.EntryViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.entry_item_constraints, parent, false);
EntryViewHolder vh = new EntryViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(EntryViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.title.setText(mDataset[position].getTitle());
holder.label.setText(mDataset[position].getLabel());
holder.textContent.setText(mDataset[position].getTextContent());
holder.thumbnail.setImageDrawable(LoadImageFromWebOperations(mDataset[position].getThumbnail()));
holder.author.setText(mDataset[position].getAuthor());
//new DownloadImageTask(holder.thumbnail).execute(mDataset[position].getThumbnail());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "reddit_thumbnail");
return d;
} catch (Exception e) {
return null;
}
}
}