我的适配器
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.news_list_adapter,new String[] {TAG_NAME, TAG_ADDRESS, R.drawable.news_en2},
new int[] {R.id.newsHead, R.id.qisaIzzah ,R.id.newsFontImage});
我想要添加到列表中的位图图像,我可以编写什么而不是R.drawable.news_en2
示例位图
byte[] decodedString = Base64.decode(news.getString("newsimage"),Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
答案 0 :(得分:0)
通过扩展BaseAdapter类,您可以轻松地在ListView中添加位图图像
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
// if it's not recycled, initialize some attributes
if (row == null) {
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.menucategory, null);
//imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
}
// getCategoryItem
Category category = (Category) getItem(position);
// Get reference to ImageView
categoryIcon = (ImageView) row.findViewById(R.id.category_image);
//categoryIcon.setLayoutParams(new GridView.LayoutParams(85, 85));
categoryIcon.setScaleType(ImageView.ScaleType.CENTER_CROP);
categoryIcon.setPadding(5, 5, 5, 5);
categoryName = (TextView) row.findViewById(R.id.category_name);
//Set category name
categoryName.setText(category.getCategoryName());
if(category.getCategoryPath() != null){
Bitmap bitmap = BitmapFactory.decodeFile(category.getCategoryPath());
System.out.println("bitmap2:: "+ bitmap);
if(bitmap != null){
categoryIcon.setImageBitmap(bitmap);
}
}
return row;
}
答案 1 :(得分:0)
您还必须定义一个可以获取位图的布局。在由Mohammod Hossain发布的上述示例中,R.layout.menucategory指的是该自定义布局。
答案 2 :(得分:0)
基本上,简单适配器会自动将一些资源ID或URI绑定到行布局的imageview。 但它不支持Bitmap。
这是一个问题,因为每个必须管理位图的人都知道我们经常需要减小图片的大小以防止outOfMemory异常。 但是,如果要将图像添加到listView中,如果仅提供URI,则无法缩小图像的大小。所以这是解决方案:
我修改了simpleAdapter以便能够处理位图。 将此类添加到项目中,并使用它而不是simpleAdapter。 然后,不是传递图像的URI或ressourceId,而是传递Bitmap!
下面是代码:
import java.util.HashMap;
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 ExtendedSimpleAdapter extends SimpleAdapter{
List<HashMap<String, Object>> map;
String[] from;
int layout;
int[] to;
Context context;
LayoutInflater mInflater;
public ExtendedSimpleAdapter(Context context, List<HashMap<String, Object>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
layout = resource;
map = data;
this.from = from;
this.to = to;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return this.createViewFromResource(position, convertView, parent, layout);
}
private View createViewFromResource(int position, View convertView,
ViewGroup parent, int resource) {
View v;
if (convertView == null) {
v = mInflater.inflate(resource, parent, false);
} else {
v = convertView;
}
this.bindView(position, v);
return v;
}
private void bindView(int position, View view) {
final Map dataSet = map.get(position);
if (dataSet == null) {
return;
}
final ViewBinder binder = super.getViewBinder();
final int count = to.length;
for (int i = 0; i < count; i++) {
final View v = view.findViewById(to[i]);
if (v != null) {
final Object data = dataSet.get(from[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 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 {
throw new IllegalStateException(v.getClass().getName() +
" should be bound to a Boolean, not a " +
(data == null ? "<unknown type>" : 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 {
setViewImage((ImageView) v, text);
}
} else {
throw new IllegalStateException(v.getClass().getName() + " is not a " +
" view that can be bounds by this SimpleAdapter");
}
}
}
}
}
private void setViewImage(ImageView v, Bitmap bmp){
v.setImageBitmap(bmp);
}
}
此类的行为与原始类(SimpleAdapter)
完全相同