如果我在解释问题时误会,请原谅我。我有一个自定义基础适配器,其中有两个imageView和两个TextView,我正在使用异步任务来设置URL中的图像。它会设置图像,但会自动再次更改图像。
下面是适配器的代码。
public class SharedPhotosAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
public static ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
private static final String BRANCH="Branch";
private static final String DATE="DateTime";
private static final String STARS="Stars";
private static final String IMAGE_URL="URL";
private static final String USER_NAME="UserName";
TextView name,date,comment;
ImageView pro_image,shared_image;
public SharedPhotosAdapter(Context con,ArrayList<HashMap<String, String>> result) {
// TODO Auto-generated constructor stub
context=con;
data=result;
inflater = (LayoutInflater)context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View rowView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
Bitmap bitmap=null;
ImageView image = null;
HashMap<String, String> result=data.get(position);
if(rowView==null)
{
rowView=inflater.inflate(R.layout.shared_photos_item, null);
holder=new ViewHolder();
holder.name=(TextView)rowView.findViewById(R.id.textView1);
holder.date=(TextView)rowView.findViewById(R.id.textView4);
holder.comment=(TextView)rowView.findViewById(R.id.textView3);
holder.pro_image=(ImageView)rowView.findViewById(R.id.imageView1);
holder.shared_image=(ImageView)rowView.findViewById(R.id.imageView2);
rowView.setTag(holder);
}
else
{
holder = (ViewHolder)rowView.getTag();
}
new DownloadImageTask(holder.shared_image).execute(result.get(IMAGE_URL));
holder.name.setText(result.get(USER_NAME));
holder.date.setText(result.get(DATE));
holder.comment.setText(result.get(BRANCH));
return rowView;
}
public class ViewHolder
{
TextView name,date,comment;
ImageView pro_image,shared_image;
}
}
以下是我用于从网址设置图片的异步任务
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
}
return mIcon11;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
bmImage.setImageBitmap(result);
}
}
它随机将任何图像设置到图像视图,只是无法弄清楚我哪里出错了。 一点帮助都会很明显。
感谢
答案 0 :(得分:3)
我建议您尝试使用Picasso来消除下载图片/缓存等麻烦。 一旦你的工作区中有了jar,你需要在适配器类的getView()方法中做的就是这个。
Picasso.with(context).load(result.get(IMAGE_URL)).into(holder.shared_image);
不需要DownloadAsyncTask。 正如@zapl在评论中也提到的那样,还有像Volley和UniversalImageLoader这样的其他库,但我喜欢Picasso。您还可以使用Picasso提供的Transformation接口轻松应用round-image等转换。