如果图片尚未存在,我会尝试下载图片,如果已经下载了图片,请保留并重复使用。
在下载和显示工作时,重复使用它们不会。在不同的调用中,mValues
似乎不是mValues
。
这是我的代码
public void onBindViewHolder(final TabFragment1.SimpleItemRecyclerViewAdapter.ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).title);
holder.mContentView.setText(mValues.get(position).description);
if (holder.mThumbnail == null) {
System.out.println("thumbnail null"); //NEVER gets called
} else {
if(mValues.get(position).thumbnailData!=null){
System.out.println("thumbnailData contained"); //NEVER GETS CALLED, IT ALWAYS SEEMS TO BE NUL
holder.mThumbnail.setImageDrawable(mValues.get(position).thumbnailData);
} else {
System.out.println("thumbnailData downloading"); //always happening
try {
AsyncTask<String, Void, Bitmap> execute = new DownloadImageTask((ImageView) holder.mThumbnail)
.execute(holder.mItem.thumbnailUrl);
mValues.get(position).thumbnailData = holder.mThumbnail.getDrawable();
if(mValues.get(position).thumbnailData!=null) {
System.out.println("is null"); //hardly ever gets printed
} else {
System.out.println("is not null"); //always gets printed
}
} catch (Exception ex) {
}
}
}
}
解决:我的代码错了。缩略图更新并没有等待AsyncTask完成,我搞砸了if语句,所以它欺骗我思考&#34; new&#34;缩略图工作。我将缩略图更新放入AsyncTask中,现在它正在工作:)
答案 0 :(得分:2)
你的if else语句,在try catch里面已经互换了日志语句......这里你的if语句检查(thumbnail!= null)但如果发现是真的那么打印为null ....所以我的观点是,你的缩略图object总是为null,因为它的值取决于你刚刚在它上面启动的Asynctask的完成,所以你不能像这样访问它,因为这个下载过程将在不同的线程上运行。
答案 1 :(得分:0)
您可以使用Picasso库来安装android,这将为您缓存图像和其他很酷的东西。
答案 2 :(得分:0)