在自定义窗口小部件HorizontalListView中,我显示来自URL的图像。
问题是,只有当项目第二次出现在屏幕上时,才会从URL加载此ListView图像中的每个项目,这可能意味着在视图被回收之后。为什么第一次显示查看项目时未加载URL图像我不知道。首先想到的可能是自定义视图问题,但我尝试使用常规ListView以相同的问题结束。
使用我在网上找到的ImageLoader加载图片。我现在已经坚持了大约7-8小时,任何帮助都会受到赞赏
这是适配器:
public class ItemPreviewAdapter extends ArrayAdapter<HashMap<String, String>> {
private final List<HashMap<String, String>> items;
private final LayoutInflater mLayoutInflater;
ImageLoader imgLoader;
static class ViewHolder {
TextView item_name;
TextView item_price;
ImageView image;
}
public ItemPreviewAdapter(final Context context, final int textViewResourceId, List<HashMap<String, String>> map) {
super(context, textViewResourceId);
mLayoutInflater = LayoutInflater.from(context);
imgLoader = new ImageLoader(context);
this.items = map;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.lvitem_homepage_item, parent, false);
holder = new ViewHolder();
holder.item_name = (TextView) convertView.findViewById(R.id.tvItemName);
holder.item_price = (TextView) convertView.findViewById(R.id.tvItemPrice);
holder.image = (ImageView) convertView.findViewById(R.id.ivItemImage);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
HashMap<String, String> item = items.get(position);
String id = item.get("id");
String item_name = item.get("iname").toUpperCase();
String item_price = item.get("new_price").toUpperCase();
String currency = item.get("currency").toUpperCase();
holder.item_name.setText(item_name);
holder.item_price.setText(item_price+" "+currency);
String image_url = "http://www.mywebsite.com/images/items/cropped_image_list_id_"+ id + ".png";
imgLoader.DisplayImage(image_url, holder.image);
return convertView;
}
}
ImageLoader的:
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
final int stub_id = R.drawable.ic_launcher;
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
// imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
// from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 1;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
@Override
public void run() {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
Object tag = photoToLoad.imageView.getTag();
if(tag != null && ((String)tag).equals(photoToLoad.url)){
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
// Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
public void run() {
if (imageViewReused(photoToLoad))
return;
if (bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
任务:
private class LatestItems extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... loc_id) {
// For storing data from web service
String data = "";
// Downloader object
UrlDownloader downloader = new UrlDownloader();
// Building the url to the web service
String url = "http://www.mywebsite.com/web_services/home_screen_latest_items.php?loc_id="
+ loc_id[0];
try {
// Fetching the data from web service in background
data = downloader.downloadUrl(url);
} catch (Exception e) {
Log.d("Downloader exception", e.toString());
}
return data;
}
@Override
protected void onPostExecute(String downloadedData) {
super.onPostExecute(downloadedData);
//If web service returned data
if (downloadedData != null) {
JSONObject jObject;
// Instantiate parser
ItemJSONParser itemsParser = new ItemJSONParser();
try {
// Put data into JSON Object and pass it to parser
jObject = new JSONObject(downloadedData);
items = itemsParser.parse(jObject);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
// Instantiate adapter. Provide Context, Item layout and
// DataSource (result)
ItemPreviewAdapter itemPreview = new ItemPreviewAdapter(
getApplicationContext(), R.layout.lvitem_homepage_item,
items);
// Pass each data to from list into to adapter
for (HashMap<String, String> data : items) {
itemPreview.add(data);
}
// Set adapter
latestItemsList.setAdapter(itemPreview);
}
}
}
答案 0 :(得分:0)
在asynctask的onPostExecute()中设置适配器后尝试执行itemPreview.notifyDataSetChanged()
。
答案 1 :(得分:0)
此处您没有将列表绑定到父级:
替换
public ItemPreviewAdapter(final Context context, final int textViewResourceId, List<HashMap<String, String>> map) {
super(context, textViewResourceId);
....
}
与
public ItemPreviewAdapter(final Context context, final int textViewResourceId, List<HashMap<String, String>> map) {
super(context, textViewResourceId,map);
....
}
还有一件事
if (convertView == null) {
.....
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
在获得holdertag时添加其他..
多数民众赞成......