Android: ArrayAdapter isnt setting images properly
Inside a ListFragment I have a ListView which contains an image and some
text (See Previous Solved Question)
I briefly managed to get the solution to that question to work but its
stopped working now. I'm loading image Bitmaps in an AsyncTask which
update the Adapter via adapter.notifyDataSetChanged();
when they finish downloading I store the Bitmap in an class member: Bitmap
array named ICONS[] which I access directly from the ArrayAdapter to save
memory.
There's various checks in place to make sure i'm not downloading images
multiple times all of which are working but ill add them here anyway:
note i here is each entry in the ListView
ICONS[i] = null for all i until they are downloaded and ICONS[i] becomes a
Bitmap image.
iconSet[i] = false for all i until image is downloaded and set (updated
within adapter), this stops the resetting of the ImageView Bitmap if its
already been set.
iconDownloading[i] = false for all i until the AsyncTask downloader starts
iconDownloaded[i] = false for all i until the AsyncTask downloader finishes
private class ArticleAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public ArticleAdapter(Context context, String[] values) {
super(context, R.layout.list_entry, values);
this.context=context;
this.values=values;
}
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView =
inflater.inflate(R.layout.list_entry,parent,false);
TextView tv = (TextView) rowView.findViewById(R.id.label);
tv.setText(values[position]);
if(ICONS[position]!=null&&!iconSet[position]){//not null and
not set
iconSet[position]=true;
System.out.println("ArticleListFragment: Page " +
pageIndex + " Setting icon " + position);
ImageView iv = (ImageView) rowView.findViewById(R.id.logo);
iv.setImageBitmap(ICONS[position]);
}else if(!iconDownloading[position]&&!iconDownloaded[position]){
iconDownloading[position]=true;//Starting download
String iconUrl =
ARTICLE_LIST.getArticle(position).getThumbnail();
if(iconUrl!=null&&!iconUrl.equals("")){
System.out.println("ArticleListFragment: Page " +
pageIndex + " Downloading icon " + position);
new LoadThumbnail(position, iconUrl).execute();
}else{
System.out.println("ArticleFragment: Article " +
position + " has no icon");
}
}
//iv.setImageResource(resId);//Change Image - All set to sport
at the moment
return rowView;
}
}
I've added a few System.out calls aswell to check everything is been done
in the correct order, as intended the:
System.out.println("ArticleListFragment: Page " + pageIndex + "
Downloading icon " + position);
Comes first then the:
System.out.println("ArticleListFragment: Page " + pageIndex + " Setting
icon " + position);
Which is right before the setting of the Bitmaps, so its getting to the
right point, with the right Bitmap (i assume as i've used
ICONS[position].size() which outputs about 4000 bytes) so i'm stuck
wondering what's going wrong?
EDIT Ive narrowed the problem down to the check if(!iconSet[position]) if
i remove this the bitmaps are loaded correctly, but they are reloaded
every time another bitmap is added to the ICONS array which for some
reason triggers that if statement 4 times for 2 icons.
Why is it doing it 4 times? (twice per row entry) and why does that check
make a difference?
No comments:
Post a Comment