ListView通过自定义的Adapter实现异步下载显示网络图片

先说思路,首先让一张放在res/drawable里的图片代替网络图片,加进ListItem,现在显示的就是本地图片,然后新开一个线程循环下载网络图片,每下完一张,替换原来显示的本地图片,更新ListView。
刚开始做的时候,乱配器用的是SimpleAdapter,但是后来发现,SimpleAdapter只能加载放事先放在res/drawable,不能加载Bitmap,或者是Drawable,于是自己写了个适配器来继承SimpleAdapter,加入Bitmap支持,完美实现以上功能。


		final ListView list = (ListView) findViewById(R.id.favlv);
		list.setDivider(null);
		list.setOnItemClickListener(Click);
		
		myAdapter listItemAdapter;
		ArrayList> listItem = new ArrayList>();
		cursor=db.query("fav",new String[]{"id","title","url","thumb"},null,null,null,null,"id DESC");
		UrlArr.clear();
		ThumbArr.clear();
		ItemID.clear();
		if(cursor.getCount()>0){
			nofav.setVisibility(View.INVISIBLE);
		}
		while(cursor.moveToNext()){ 
            HashMap map = new HashMap();  
            map.put("ItemImage",R.drawable.load);//图像资源的ID  
            map.put("ItemTv",cursor.getString(1));
            listItem.add(map);
            UrlArr.add(cursor.getString(2));
            ThumbArr.add(cursor.getString(3));
            ItemID.add(cursor.getString(0));
        }
		cursor.close();
		//以上内容是读取数据库,并把数据加到listItem上,没有数据库的自己看着改,ThumbArr存放的是图片URL,另两个是我项目里用的
        listItemAdapter = new myAdapter(this,listItem,R.layout.favlv,new String[] {"ItemImage","ItemTv"},new int[] {R.id.ItemImage,R.id.ItemTv});  
        list.setAdapter(listItemAdapter);
        list.setOnCreateContextMenuListener(MenuLis);
        //新开线程循环下载图片
        new Thread(){
        	@SuppressWarnings("unchecked")
			public void run(){
        		int i=0;
        		while(i map = (HashMap)listItemAdapter.getItem(i);
						map.put("ItemImage",bmp);			
						handler.sendEmptyMessage(0);//这个是更新通知就是listItemAdapter.notifyDataSetChanged();

					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					i++;
        		}
        	}
        	
        }.start();

下面的是myAdapter适配器代码:


package com.BottomMenu;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class myAdapter extends SimpleAdapter {

	private int[] mTo;
    private String[] mFrom;
    private ViewBinder mViewBinder;

    private List> mData;

    private int mResource;
    private int mDropDownResource;
    private LayoutInflater mInflater;
	
	public myAdapter(Context context,
			List> data, int resource, String[] from,
			int[] to) {
		super(context, data, resource, from, to);
		mData = data;
        mResource = mDropDownResource = resource;
        mFrom = from;
        mTo = to;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	
	 /**
     * @see android.widget.Adapter#getView(int, View, ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, mResource);
    }

    private View createViewFromResource(int position, View convertView,
            ViewGroup parent, int resource) {
        View v;
        if (convertView == null) {
            v = mInflater.inflate(resource, parent, false);

            final int[] to = mTo;
            final int count = to.length;
            final View[] holder = new View[count];

            for (int i = 0; i < count; i++) {
                holder[i] = v.findViewById(to[i]);
            }

            v.setTag(holder);
        } else {
            v = convertView;
        }

        bindView(position, v);

        return v;
    }
    
    private void bindView(int position, View view) {
        final Map dataSet = mData.get(position);
        if (dataSet == null) {
            return;
        }

        final ViewBinder binder = mViewBinder;
        final View[] holder = (View[]) view.getTag();
        final String[] from = mFrom;
        final int[] to = mTo;
        final int count = to.length;

        for (int i = 0; i < count; i++) {
            final View v = holder[i];
            if (v != null) {
                final Object data = dataSet.get(from[i]);
//                if(data i)
                String text = data == null ? "" : data.toString();
                if (text == null) {
                    text = "";
                }

                boolean bound = false;
                if (binder != null) {
                    bound = binder.setViewValue(v, data, text);
                }

                if (!bound) {
                    if (v instanceof Checkable) {
                        if (data instanceof Boolean) {
                            ((Checkable) v).setChecked((Boolean) data);
                        } else {
                            throw new IllegalStateException(v.getClass().getName() +
                                    " should be bound to a Boolean, not a " + data.getClass());
                        }
                    } else if (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the bottom of these
                        // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                        setViewText((TextView) v, text);
                    } else if (v instanceof ImageView) {
                    	
                        if (data instanceof Integer) {
                            setViewImage((ImageView) v, (Integer) data);                            
                        } else if(data instanceof Bitmap) {
                            setViewImage((ImageView) v, (Bitmap)data);
                        }
                    } else {
                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleAdapter");
                    }
                }
            }
        }
    }

    public void setViewImage(ImageView v, int value) {
        v.setImageResource(value);
    }


    public void setViewImage(ImageView v, Bitmap bm) {
    	((ImageView) v).setImageBitmap(bm);
    }
	
};

© 2011, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记