Android ImageView被GridView回收
我有一个Android ImageView被GridView回收,android,android-imageview,android-gridview,android-adapter,Android,Android Imageview,Android Gridview,Android Adapter,我有一个GridView,其中包含一个适配器,它扩展了ArrayAdapter(其中Album是从lastfmapi获取的唱片集信息) 相册信息附带图像的URL。我异步下载这些图像,并将它们与唱片集名称一起放入适配器中 但是,当我向下和向上滚动GridView时,行被回收。当我再次充气时,图像被交换,我真的不知道发生了什么 我尝试将图像存储在HashMap中,但没有效果 这是我的适配器的代码 package com.gigtracker.adapter; import java.text.De
GridView
,其中包含一个适配器
,它扩展了ArrayAdapter
(其中Album是从lastfmapi获取的唱片集信息)
相册
信息附带图像的URL。我异步下载这些图像,并将它们与唱片集名称一起放入适配器中
但是,当我向下和向上滚动GridView
时,行被回收。当我再次充气时,图像被交换,我真的不知道发生了什么
我尝试将图像存储在HashMap
中,但没有效果
这是我的适配器的代码
package com.gigtracker.adapter;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.garli.lastfmapi.Album;
import com.garli.lastfmapi.Artist;
import com.garli.lastfmapi.ImageSize;
import com.gigtracker.R;
import com.gigtracker.utils.ImageDownloader;
public class AlbumAdapter extends ArrayAdapter<Album> {
private final ImageDownloader d;
int playcount;
@SuppressLint("UseSparseArrays")
private final HashMap<Integer, View> map = new HashMap<Integer, View>();
public AlbumAdapter(final Context ctx, final Artist artist) {
super(ctx, 0);
d = new ImageDownloader();
new AsyncTask<Void, Void, Void>() {
CopyOnWriteArrayList<Album> albums = new CopyOnWriteArrayList<Album>();
@Override
protected Void doInBackground(final Void... params) {
final Collection<Album> albumCollection = Artist
.getTopAlbums(artist.getName());
for (final Album album : albumCollection) {
if (playcount < album.getPlaycount()) {
playcount = album.getPlaycount();
}
albums.add(album);
}
return null;
}
@Override
protected void onPostExecute(final Void result) {
for (final Album album : albums) {
add(album);
}
notifyDataSetChanged();
}
}.execute();
}
@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
final TextView tv, tv2;
final ImageView iv;
final Album album = getItem(position);
if (map.get(Integer.valueOf(position)) == null) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.grid_item, null);
tv = (TextView) convertView.findViewById(R.id.textViewName);
iv = (ImageView) convertView.findViewById(R.id.imageView1);
tv2 = (TextView) convertView.findViewById(R.id.textViewRating);
@SuppressWarnings("deprecation")
int buttonDimension = ((Activity) getContext())
.getWindowManager().getDefaultDisplay().getWidth() / 2;
final int paddingTotal = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 20, getContext()
.getResources().getDisplayMetrics());
buttonDimension -= paddingTotal;
iv.getLayoutParams().height = buttonDimension;
iv.getLayoutParams().width = buttonDimension;
new AsyncTask<Void, Void, Void>() {
Bitmap bmp;
@Override
protected Void doInBackground(final Void... params) {
bmp = d.downloadBitmap(album
.getImageURL(ImageSize.LARGE));
return null;
}
@Override
protected void onPostExecute(final Void result) {
iv.setImageBitmap(bmp);
}
}.execute();
} else {
tv = (TextView) convertView.findViewById(R.id.textViewName);
tv2 = (TextView) convertView.findViewById(R.id.textViewRating);
}
} else {
convertView = map.get(Integer.valueOf(position));
tv = (TextView) convertView.findViewById(R.id.textViewName);
tv2 = (TextView) convertView.findViewById(R.id.textViewRating);
}
tv.setText(album.getName());
final float percentage = 100 * (float) album.getPlaycount() / playcount;
tv2.setText(new DecimalFormat("##0.00").format(percentage) + "%");
if (percentage >= 90) {
tv2.setTextColor(0xff99ff33);
} else if (percentage >= 75) {
tv2.setTextColor(0xffffff66);
} else if (percentage >= 50) {
tv2.setTextColor(0xffff9900);
} else if (percentage >= 25) {
tv2.setTextColor(0xffcc6600);
} else {
tv2.setTextColor(0xffcc0000);
}
return convertView;
}
}
package com.gigtracker.adapter;
导入java.text.DecimalFormat;
导入java.util.Collection;
导入java.util.HashMap;
导入java.util.concurrent.CopyOnWriteArrayList;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.os.AsyncTask;
导入android.util.TypedValue;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
导入com.garli.lastfmapi.Album;
导入com.garli.lastfmapi.Artist;
导入com.garli.lastfmapi.ImageSize;
导入com.gigtracker.R;
导入com.gigtracker.utils.ImageDownloader;
公共类AlbumAdapter扩展了ArrayAdapter{
私人最终图像下载程序d;
int播放计数;
@SuppressLint(“UseSparsearray”)
private final HashMap map=new HashMap();
公共相册适配器(最终上下文ctx,最终艺术家){
super(ctx,0);
d=新图像下载程序();
新建异步任务(){
CopyOnWriteArrayList相册=新建CopyOnWriteArrayList();
@凌驾
受保护的Void doInBackground(最终Void…参数){
最终收藏albumCollection=艺术家
.getTopAlbums(artist.getName());
用于(最终专辑:albumCollection){
if(playcount=90){
tv2.setTextColor(0xff99ff33);
}否则,如果(百分比>=75){
tv2.setTextColor(0xffffff66);
}否则,如果(百分比>=50){
tv2.setTextColor(0xffff9900);
}否则,如果(百分比>=25){
tv2.setTextColor(0xffcc6600);
}否则{
tv2.setTextColor(0xffcc0000);
}
返回视图;
}
}
应使用图案视图夹:
public class NewHolder {
public ImageView ivIcon;
public TextView tvTitle;
public int position;
}
三,
公共类LazyLoadImage扩展异步任务{
private final String TAG = "LazyLoadThumbnail";
private NewHolder holder = null;
private UBRNew ubrNew = null;
private int position = 0;
private AdapterUbrNews adapter = null;
private int widthIcon = 0;
private int heightIcon = 0;
private DBNews db = null;
private Context context = null;
public LazyLoadImage(Context context,
AdapterUbrNews adapter,
UBRNew ubrNew,
NewHolder holder,
int position,
int widthIcon,
int heightIcon) {
this.adapter = adapter;
this.holder = holder;
this.position = position;
this.ubrNew = ubrNew;
this.widthIcon = widthIcon;
this.heightIcon = heightIcon;
this.ubrNew.setDownloadedIcon(true);
db = DBNews.getInstance(context);
this.context = context;
}
@Override
protected Bitmap doInBackground(Void... voids) {
Bitmap result = null;
try {
Bitmap temp = download(ubrNew.getUrlIcon());
if (temp != null) {
result = Bitmap.createScaledBitmap(temp, widthIcon, heightIcon, true);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
ubrNew.setIcon(bitmap);
if (position == holder.position) {
holder.ivIcon.setImageBitmap(bitmap);
} else {
adapter.notifyDataSetChanged();
}
} else {
ubrNew.setDownloadedIcon(false);
}
}
private Bitmap download(String urlString) throws MalformedURLException, IOException {
if ((urlString == null) || (urlString.equals(""))) {
return null;
}
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(urlString);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
return BitmapFactory.decodeStream(inputStream);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
} catch (Exception e) {}
return null;
}
}
代码段中LazyLoadImage中的所有功能:
if (position == holder.position) {
holder.ivIcon.setImageBitmap(bitmap);
} else {
adapter.notifyDataSetChanged();
}
我到家后会测试一下,现在我有点忙。我没有忘记给你接受的答案,只是我现在无法测试;-)好啊如果您有问题,请通过电子邮件(在我的个人资料中)给我写信
if (position == holder.position) {
holder.ivIcon.setImageBitmap(bitmap);
} else {
adapter.notifyDataSetChanged();
}