我有一个ListView来显示一个标题较短的照片库(ImageView + TexiView)。我使用AsyncTask来执行下载任务。但问题是有时它会显示错误的图像然后突然改变(可能会发生很多次)。 例如:IA B C将在ListView中显示,图像m1 m2 m3分别用于ABC。 当我运行应用程序时,imageView for A可能会显示m1,m2或m3并经常更改。 这是我的代码:
DownClass:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage = null;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
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) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
if(result == null){
bmImage.setImageResource(R.drawable.loading);
}else{
bmImage.setImageBitmap(result);
}
}
}
ListView适配器:
public View getView(int position, View convertView, ViewGroup parent){
item item1 = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item1, parent, false);
}
ImageView iv = (ImageView) convertView.findViewById(R.id.img);
TextView ttv = (TextView) convertView.findViewById(R.id.p_title);
TextView itv = (TextView) convertView.findViewById(R.id.p_price);
/**
*Download Image
*/
String img_url;
img_url = item1.getImg_url();
new DownloadImageTask((ImageView) convertView.findViewById(R.id.img))
.execute(img_url);
ttv.setText(item1.getTitle());
itv.setText("$"+item1.getPrice());
return convertView;
}
活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_promotion2);
gridView = (GridView) findViewById(R.id.gridview);
getPromotionByRequest(previousPage, data);//Get Data for items
itemAdapter = new promotionAdapter(this, items);
//progress..........
gridView.setAdapter(itemAdapter);
setupListViewListener();
}
答案 0 :(得分:1)
感谢您的回答,遗憾的是问题仍然存在。 最后,我确实通过在每个适配器中使用“Picasso”来解决它,如此强大
答案 1 :(得分:0)
尝试使用getview的视图持有者
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(android.R.layout.list_item_recyclerView, parent, false);
return new ViewHolder(view);
}
OrElse运算 也可以在getview中尝试这个 创建layoutinflator:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
并创建另一个视图参考:
然后夸大儿童观点:
View v = convertView;
v = mInflater.inflate(R.layout.layout_namehere, parent, false);
TextView name = (TextView) view.findViewById(R.id.name);
答案 2 :(得分:0)
public View getView(int position, View convertView, ViewGroup parent){
item item1 = getItem(position);
ViewHolder vh = null;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item1, parent, false);
vh.iv = (ImageView) convertView.findViewById(R.id.img);
vh.ttv = (TextView) convertView.findViewById(R.id.p_title);
vh.itv = (TextView) convertView.findViewById(R.id.p_price);
convertView.setTag(vh);
}else{
vh = (ViewHoldler)convertView.getTag();
}
/**
*Download Image
*/
String img_url;
img_url = item1.getImg_url();
new DownloadImageTask((ImageView) convertView.findViewById(R.id.img))
.execute(img_url);
ttv.setText(item1.getTitle());
itv.setText("$"+item1.getPrice());
return convertView;
}
public class ViewHolder {
public ImageView iv;
public TextView ttv;
public TextView itv;
}
也许这是不正确的,你可以尝试......我已经很长时间没有列表视图了
答案 3 :(得分:0)
在getview()方法中创建视图持有者类,如bellow。
ViewHolder holder = new ViewHolder();
然后将视图添加到持有者,如下面的代码
holder.imageviewname= (ImageView) convertView.findViewById(R.id.listitem_image);
holder.textviewname = (TextView) convertView.findViewById(R.id.listitem_text);
有关详细信息,请参阅文档https://developer.android.com/training/improving-layouts/smooth-scrolling.html