Asynchronous image loading dislocation

Source: Internet
Author: User

=============== Problem description ====================


I wrote a public Adapter. The problem is that the image of the first item is always incorrect after each loading. Then, I commented out if (converview = NULL) in getview, and then I can create another item every time. Therefore, performance must be affected. PS: I am an authentic cainiao. I only know a little about non-computer-specialized Java, so we should not use too many terms. Thank you ~
So paste the code ~
package com.test.sztestagain.adapter;import java.util.ArrayList;import java.util.List;import java.util.Map;import com.test.sztestagain.imageUitl.AsyncImageLoader;import com.test.sztestagain.imageUitl.CallBackImpl;import android.content.Context;import android.graphics.drawable.Drawable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyListAdapter extends BaseAdapter {private LayoutInflater layoutInflater;private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();private int resource;private AsyncImageLoader loader = new AsyncImageLoader();private String[] text;private int[] id;private String image;private int imageId;public MyListAdapter(Context context, List<Map<String, Object>> list,int resource, String[] text, int[] id, String image, int imageId) {this.list = list;this.resource = resource;this.text = text;this.id = id;this.image = image;this.imageId = imageId;layoutInflater = LayoutInflater.from(context);}public MyListAdapter(Context context, List<Map<String, Object>> list,int resource, String[] text, int[] id) {this.list = list;this.resource = resource;this.text = text;this.id = id;layoutInflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return this.list != null ? this.list.size() : 0;}@Overridepublic Object getItem(int position) {return this.list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// if (convertView == null) {convertView = layoutInflater.inflate(resource, parent, false);// }if (image != null && !"".equals(image)) {ImageView imageView = (ImageView) convertView.findViewById(imageId);CallBackImpl callBackImpl = new CallBackImpl(imageView);Drawable cacheImage = loader.loadDrawable(list.get(position).get(image).toString(), callBackImpl);if (cacheImage != null) {imageView.setImageDrawable(cacheImage);}}for (int i = 0; i < id.length; i++) {TextView tv = (TextView) convertView.findViewById(id[i]);tv.setText(list.get(position).get(text[i]).toString());}return convertView;}}

============= Solution 1 ======================


Methods to avoid repeated loading:
Store all items in the array, and add an item in the array to indicate whether the items have been loaded. values 0: not loaded, 1: loaded, in getview, the value of this flag is determined every time. If it is 0, it is reloaded and the value is changed to 1. If it is 1, it is not loaded.

Asynchronous image loading dislocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.