SoFunction
Updated on 2025-03-07

ListView Adapter Optimization Example

Copy the codeThe code is as follows:

//Scheme 1 - adapter optimization public class listviewAdapterDemo1 extends BaseAdapter {

    private String[] mArrData;
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;
    private final LayoutInflater inflater;

    public listviewAdapterDemo1(String[] mArrData, Context mContext, ListView listView) {
        = mArrData;
        = listView;
        asyncImageLoader = new AsyncImageLoader();
        inflater = (LayoutInflater) (Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return ;
    }

    @Override
    public Object getItem(int arg0) {
        return mArrData[arg0];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /***
* 1. The control object memory is one
* Objects will only create visual objects (some 8 times) depending on the number of visual listviews on the screen, and will not be recreated when more than visual post data is loaded.
*     But the data needs to be reloaded every time
     *  
*    From the perspective of saving memory, this is more reasonable. After all, loading data does not require memory occupancy
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewholder;
        ("dd", "Position:" + position+"------");
        if (convertView == null) {
            ("dd", "Position:" + position);
            convertView = (.list_item_icon_text, null);
            viewholder = new ViewHolder();
            = (ImageView) ();
            = (TextView) ();

            (viewholder);
        } else {
            viewholder = (ViewHolder) ();
        }

        (mArrData[position]);
        // Load the image and set it on the ImageView
        String imageUrl = "";
        (imageUrl);
        Drawable cachedImage = (imageUrl, new ImageCallback() {

            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                ImageView imageViewByTag = (ImageView) (imageUrl);
                if (imageViewByTag != null) {
                    (imageDrawable);
                }
            }
        });
if (cachedImage != null)//Indicates that it is obtained from the cache pool
            (cachedImage);

        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        TextView text;
    }
}


This method can also be used in the getview method in the listview:

Copy the codeThe code is as follows:

public Drawable getDrawable(AsyncImageLoader asyncImageLoader, String imageUrl, final ImageView imageView) {
Drawable drawable = (imageUrl,new ImageCallback() {
@Override public void imageLoaded(Drawable imageDrawable,String imageUrl) {
if (imageDrawable != null)
(imageDrawable);
else
(.u6_normal);
}
});
return drawable;
}

Copy the codeThe code is as follows:

//Scheme 2----Optimization of adapter public class listviewAdapterDemo2 extends BaseAdapter {

    private String[] mArrData;
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;
    private final LayoutInflater inflater;
    private Map<Integer, View> viewMap = new HashMap<Integer, View>();

    public listviewAdapterDemo2(String[] mArrData, Context mContext, ListView listView) {
        = mArrData;
        = listView;
        asyncImageLoader = new AsyncImageLoader();
        inflater = (LayoutInflater) (Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return ;
    }

    @Override
    public Object getItem(int arg0) {
        return mArrData[arg0];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
* 1. The control object memory is one
* 2. Each post corresponds to a view, and regardless of forward and backward, each post data is only loaded once. * However, from the perspective of saving content, the first plan saves memory
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewholder;
        ("dd", "Position:" + position+"------");
        convertView = (position);
        if (convertView == null) {
            ("dd", "Position:" + position);
            convertView = (.list_item_icon_text, null);
            viewholder = new ViewHolder();
            = (ImageView) ();
            = (TextView) ();

            (mArrData[position]);
            // Load the image and set it on the ImageView
            String imageUrl = "";
            (imageUrl);
            Drawable cachedImage = (imageUrl, new ImageCallback() {

                public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                    ImageView imageViewByTag = (ImageView) (imageUrl);
                    if (imageViewByTag != null) {
                        (imageDrawable);
                    }
                }
            });
if (cachedImage != null)//Indicates that it is obtained from the cache pool
                (cachedImage);

           
            (viewholder);
            (position, convertView);
        } else {
            viewholder = (ViewHolder) ();
        }
        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        TextView text;
    }
}

Copy the codeThe code is as follows:

/**Picture download-adpater optimization*/
public class AsyncImageLoader {

    private HashMap<String, SoftReference<Drawable>> imageCache; 

    public AsyncImageLoader() { 
        imageCache = new HashMap<String, SoftReference<Drawable>>(); 
    } 

    public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) { 
//Check whether it exists from the cache
        if ((imageUrl)) { 
            SoftReference<Drawable> softReference = (imageUrl); 
            Drawable drawable = (); 
            if (drawable != null) { 
                return drawable; 
            } 
        } 
        final Handler handler = new Handler() { 
            @Override 
            public void handleMessage(Message message) { 
                ((Drawable) , imageUrl); 
            } 
        }; 
//If there is no cache, download it from the server
        new Thread() { 
            @Override 
            public void run() { 
                Drawable drawable = loadImageFromUrl(imageUrl); 
                (imageUrl, new SoftReference<Drawable>(drawable)); 
                Message message = (0, drawable); 
                (message); 
            } 
        }.start(); 

        return null; 
    } 

    public static Drawable loadImageFromUrl(String url) { 
        Drawable drawable = null;
//Download pictures from the server based on the url
        return drawable;
    } 

    public interface ImageCallback { 
        public void imageLoaded(Drawable imageDrawable, String imageUrl); 
    } 

Copy the codeThe code is as follows:

//activity---adapterpublic class ListviewDemoActivity extends Activity {

    private listviewAdapterDemo1 mAdapter;
    private ListView listview;
    private String[] mArrData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView();

        mArrData = new String[1000];
        for (int i = 0; i < 1000; i++) {
            mArrData[i] = "Google IO Adapter" + i;
        }

        listview = (ListView)findViewById();
        mAdapter = new listviewAdapterDemo1(mArrData,this,listview);
        (mAdapter);
    }
}

Insert in

Copy the codeThe code is as follows:

<ListView
        android:
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />