我见过几个这样的问题,但我似乎遇到了一个稍微不同的问题。所以我想我会在这里问一个人。
我有一个带有 textview 和 imageview 的自定义列表视图。我正在使用异步任务加载图像,然后在我的 post execute 方法中将图像设置为 imageview。我的图像加载正常,但某些图像被其他图像替换。但是在所有图像加载完成后,每个图像视图都有正确的图像。我似乎无法弄清楚为什么我有这个问题。
这是我的适配器和异步任务类。任何和所有的帮助将不胜感激。
编辑我现在添加一个标签(标签是进入imageviews各自textview的文本)并且在我的帖子执行中我正在检查标签是否等于来自textview的文本。我仍然遇到和以前一样的问题。
这是我的适配器类:
public class CustomListAdapter extends ArrayAdapter<CustomList> {
Context context;
int layoutResourceId;
LinkedList<CustomList> data = null;
LinkedList<String> title_list = new LinkedList();
LoadImage l;
CustomList cl;
ProgressBar pb;
HashMap <String, Bitmap> bitmap = new HashMap<String, Bitmap>();
public CustomListAdapter(Context context, int layoutResourceId, LinkedList<CustomList> data, LinkedList<Bitmap> bitmap_list) {
super(context, layoutResourceId, data);
// TODO Auto-generated constructor stub
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
CustomListHolder holder = null;
if(row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CustomListHolder();
holder.text = (TextView)row.findViewById(R.id.txtTitle);
holder.thumbnail = (ImageView)row.findViewById(R.id.imgIcon);
holder.pb = (ProgressBar)row.findViewById(R.id.progressBar1);
row.setTag(R.id.id0, holder);
cl = data.get(position);
holder.text.setText(cl.title);
holder.thumbnail.setImageResource(R.drawable.icon);
holder.pb.setVisibility(View.VISIBLE);
row.setTag(R.id.id1,new String(cl.title));
LoadImage li = new LoadImage(context, holder.thumbnail, cl.icon,cl.title, bitmap,holder.pb,row);
li.execute(cl.icon);
}
else{
Log.e("Row not null","Inside");
holder = (CustomListHolder)row.getTag(R.id.id0);
//row.getK
cl = data.get(position);
holder.text.setText(cl.title);
holder.thumbnail.setImageResource(R.drawable.icon);
holder.pb.setVisibility(View.VISIBLE);
if((Bitmap) bitmap.get(cl.title) == null){
row.setTag(R.id.id1,new String(cl.title));
LoadImage li = new LoadImage(context, holder.thumbnail, cl.icon,cl.title, bitmap,holder.pb,row);
li.execute(cl.icon);
}
else {
holder.thumbnail.setImageBitmap((Bitmap) bitmap.get(cl.title));
holder.pb.setVisibility(View.GONE);
}
}
return row;
}
static class CustomListHolder
{
ImageView thumbnail;
TextView text;
ProgressBar pb;
}
}
这是我的异步任务类:
公共类 LoadImage 扩展 AsyncTask{
Context callingContext = null;
ImageView view;
String bits;
public ProgressBar pb;
HashMap<String, Bitmap> bitmap;
String url;
String text;
View row;
public LoadImage(Context c, ImageView view, String bits, String text, HashMap<String, Bitmap> bitmap, ProgressBar pb, View row){
this.view = view;
this.bits = bits; // url for image
this.callingContext = c;
this.bitmap = bitmap; //hashmap
this.text = text;// title text
this.pb = pb;
this.row = row;
}
public Bitmap getBitmap(String data){
Bitmap bitmap;
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
Log.e("getBitmap",text);
try {
bitmap=null;
InputStream is=new URL(data).openStream();
BitmapFactory.decodeStream(is, null, bmOptions);
is.close();
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = 10;
is = new URL(data).openStream();
bitmap = BitmapFactory.decodeStream(is, null, o2);
bitmap = Bitmap.createScaledBitmap(bitmap, 60, 60, true);
is.close();
this.bitmap.put(text,bitmap);
return bitmap;
} catch (Exception ex){
Log.e("Debug", ex.getMessage());
return null;
}
}
@Override
protected Bitmap doInBackground(String... arg0) {
// TODO Auto-generated method stub
Log.e("do In Bg",text);
Bitmap b = null;
if((Bitmap)bitmap.get(text) == null)
b = getBitmap(bits);
else
b =(Bitmap)bitmap.get(text);
return b;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(row.getTag(R.id.id1).equals(text)){
view.setImageBitmap((Bitmap)bitmap.get(text));
pb.setVisibility(View.GONE);
}
}
}
CustomList 是一个具有 2 个字符串图标和标题的类。icon 是图像的 url,title 是 textview 的文本。