http://hi.baidu.com/android_frank/blog/item/a9ead4fbbfd44bdd0b46e046.html
我在一个小项目中需要用到将SD卡中的图片读出后,压缩,再进行显示。在编程过程中,使用SimpleAdapter适配器时,图片资源的ID均需是程序内的本地资源。在网上找到了两个实现方法,第一个我验证了可以实现,第二个比较繁琐还未验证。
1.实现ViewBinder()接口
一般来说,在用simpleAdapter适配器时,我们所要显示的图片资源都是程序内的本地资源,而且是以资源的ID(Resources ID)形式来表现的。但是有时候我们需要用这个适配器来显示网上的远程图片时,该如何直接显示呢?方法是实现ViewBinder()这个接口,在里面定义数据和视图的匹配关系。实例代码如下:
SimpleAdapter notes =new SimpleAdapter(this, list, R.layout.meinungen_list_row, PARAM, newint[]{ R.id.icon,R.id.name, R.id.content});
notes.setViewBinder(new MyViewBinder());
setListAdapter(notes);
publicclass MyViewBinder implements ViewBinder {
@Override
publicboolean setViewValue(View view, Object data,String textRepresentation){
if((view instanceof ImageView)&(data instanceof Bitmap)){
ImageView iv =(ImageView) view;
Bitmap bm =(Bitmap) data;
iv.setImageBitmap(bm);
returntrue;
}
returnfalse;
}
2.重写SimpleAdapter(http://www.pocketdigi.com/20110303/196.html)
myAdapter listItemAdapter;
listItemAdapter =new myAdapter(this,listItem,R.layout.favlv,newString[]{"ItemImage","ItemTv"},newint[]{R.id.ItemImage,R.id.ItemTv});
下面是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<? 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);
}
};