0

我的适配器

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); 
4

3 回答 3

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;
    }
于 2012-08-08T05:31:02.360 回答
0

您还必须定义一个可以采用位图的布局。在 Mohammod Hossain 发布的上述示例中,R.layout.menucategory 指的是该自定义布局。

于 2012-08-08T05:35:48.263 回答
0

基本上,简单的适配器会自动将一些资源 ID 或 URI 绑定到行布局的图像视图。但它不支持位图。

这是一个问题,因为每个不得不管理位图的人都知道,我们经常不得不减小图片的大小以防止出现 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) 完全相同

于 2013-03-14T16:11:47.227 回答