1. 代码段
final ListView list = (ListView) findViewById(R.id.favlv); list.setDivider(null); list.setOnItemClickListener(Click); myAdapter listItemAdapter; ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); 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<String, Object> map = new HashMap<String, Object>(); 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<ThumbArr.size()){ try { URL uri = new URL(ThumbArr.get(i)); URLConnection conn = uri.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); Bitmap bmp=BitmapFactory.decodeStream(is); is.close(); HashMap<String, Object> map = (HashMap<String, Object>)listItemAdapter.getItem(i); map.put("ItemImage",bmp); handler.sendEmptyMessage(0);//这个是更新通知就是listItemAdapter.notifyDataSetChanged(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } i++; } } }.start();
2. 适配器代码段
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<? extends Map<String, ?>> mData; private int mResource; private int mDropDownResource; private LayoutInflater mInflater; public myAdapter(Context context, List<? extends Map<String, ?>> 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); } };