我有以下问题:
我有一个由ImageAdapter驱动的gridview,它从互联网上下载图像并将它们提供给GridView。这很有效,但是有一个问题吓坏了我。问题是应该正确加载要填充屏幕的前10个图像。第11个虽然显示为空白,但由于某些未知原因,似乎直接从convertView中获取。图像12显示正确,下面的所有图像都是空白的,即使它们具有要从中下载的有效URL。
这是我的ImageAdapter
package com.wm.grid;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public String[] mThumbIds;
public Bitmap[] bmp;
public Bitmap bitmapPlaceholder;
public ImageAdapter(Context c,String[] m) {
mContext = c;
mThumbIds = m;
initBitmapListWithPlaceholders();
}
public void initBitmapListWithPlaceholders(){
int count = mThumbIds.length;
bmp = new Bitmap[count];
for(int i=0;i<count;i++){
bmp[i]=bitmapPlaceholder;
}
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("POSITION","P"+position+convertView);
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(210, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
Bitmap bm = getBitmap(setURL(mThumbIds[position]),position);
bmp[position] = bm;
imageView.setImageBitmap(bm);
} else {
imageView = (ImageView) convertView;
imageView.setImageBitmap(bmp[position]);
}
//imageView.setImageBitmap(getBitmap(setURL(mThumbIds[position])));
//imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private URL setURL (String urls){
try{
URL urli = new URL(urls);
return urli;
}
catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
private Bitmap getBitmap (URL url,int position){
try{
Log.v("POSITION","Loading "+position+url);
Bitmap bm = BitmapFactory.decodeStream((InputStream) url.getContent());
return bm;
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
/* public Integer[] AddItems(Integer[] a){
final Integer[] aThumbIds;
final int i;
aThumbIds = a;
Integer[] a2 = new Integer[mThumbIds.length + aThumbIds.length];
System.arraycopy(mThumbIds, 0, a2, 0, mThumbIds.length);
System.arraycopy(aThumbIds, 0, a2, mThumbIds.length, aThumbIds.length);
mThumbIds = a2;
return mThumbIds;
}
*/
}
未提供GridActivity.java,因为它只有一个传递给适配器的字符串(链接)数组。
main.xml只是一个gridview所以我不认为这与它有关。
我还有一些调试点的输出,这很奇怪。
11-10 14:55:28.700: V/POSITION(12936): Loading 0 //Loading the first image
11-10 14:55:28.785: V/POSITION(12936): P0android.widget.ImageView@40536450//First image found in ConvertView (why ? it is already loaded ... :()
11-10 14:55:28.790: V/POSITION(12936): P0android.widget.ImageView@40536450//Same thing
11-10 14:55:28.790: V/POSITION(12936): P1null//All OK convertView is null load image from web
11-10 14:55:28.790: V/POSITION(12936): Loading 1
11-10 14:55:28.895: V/POSITION(12936): P2null
11-10 14:55:28.895: V/POSITION(12936): Loading 2
11-10 14:55:29.070: V/POSITION(12936): P3null
11-10 14:55:29.070: V/POSITION(12936): Loading 3
11-10 14:55:29.175: V/POSITION(12936): P4null
11-10 14:55:29.175: V/POSITION(12936): Loading 4
11-10 14:55:29.290: V/POSITION(12936): P5null
11-10 14:55:29.290: V/POSITION(12936): Loading 5
11-10 14:55:29.420: V/POSITION(12936): P6null
11-10 14:55:29.420: V/POSITION(12936): Loading 6
11-10 14:55:29.500: V/POSITION(12936): P7null
11-10 14:55:29.505: V/POSITION(12936): Loading 7
11-10 14:55:29.580: V/POSITION(12936): P8null
11-10 14:55:29.580: V/POSITION(12936): Loading 8
11-10 14:55:29.675: V/POSITION(12936): P9null
11-10 14:55:29.675: V/POSITION(12936): Loading 9
11-10 14:55:29.800: V/POSITION(12936): P0null//again !? P0 was loaded in first place why does it try to load it again ?!... and it is even NULL ?!
11-10 14:55:29.800: V/POSITION(12936): Loading 0
11-10 14:55:29.910: V/POSITION(12936): P0android.widget.ImageView@40547ea0
=====================
11-10 15:02:00.435: V/POSITION(12936): P10android.widget.ImageView@40547ea0//Now image 10 was NEVER loaded before why the heck it is in the convertView !?
11-10 15:02:00.435: V/POSITION(12936): P11null//Image 11 is loaded correctly.
11-10 15:02:00.435: V/POSITION(12936): Loading 11
=====================(Scrolling down is even more questionable)
11-10 15:03:10.740: V/POSITION(12936): P12android.widget.ImageView@405505f0//Where the heck convertView found this image (never loaded)
11-10 15:03:10.740: V/POSITION(12936): P13android.widget.ImageView@40536450//Same applies for this one.
以上结果是图像10 12和13以及下面的所有图像都是空白
欢迎任何线索。非常感谢你提前。
编辑:还有一件事。如果我改变这样的代码:
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(210, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
Bitmap bm = getBitmap(setURL(mThumbIds[position]),position);
bmp[position] = bm;
imageView.setImageBitmap(bm);
} else {
imageView = (ImageView) convertView;
imageView.setImageBitmap(getBitmap(setURL(mThumbIds[position]),position));//DOWNLOAD IMAGE AGAIN
}
图像显示正确,但应用程序工作速度慢,因为它总是下载图像。