简体   繁体   English

如何将位图图像添加到ListAdapter?

[英]How can I add Bitmap image to ListAdapter?

My adapter 我的适配器

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

I have bitmap images that i want to add to the list, what can i write instead of R.drawable.news_en2 我有要添加到列表中的位图图像,我可以写些什么而不是R.drawable.news_en2

example bitmap 位图示例

byte[] decodedString = Base64.decode(news.getString("newsimage"),Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length); 

By extending BaseAdapter class you can easily add bitmap image in your ListView 通过扩展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;
    }

You will also have to define a layout which can take the bitmap. 您还必须定义一个可以使用位图的布局。 In the above example posted by Mohammod Hossain, R.layout.menucategory refers to that custom layout. 在Mohammod Hossain发布的以上示例中,R.layout.menucategory引用了该自定义布局。

Basically the simple adapter automatically bind some ressource id or URI to the imageview of your row layout. 基本上,简单适配器会自动将一些资源ID或URI绑定到行布局的imageview。 But it don't support Bitmap. 但是它不支持位图。

That's a problem, because everyone who had to manage bitmap know that we often have to reduce the size of the picture to prevent outOfMemory exceptions. 这是一个问题,因为必须管理位图的每个人都知道,我们经常必须减小图片的大小,以防止内存不足的异常。 But if you want to add images into a listView, you cannot reduce the image's size if you only provide URI. 但是,如果要将图像添加到listView中,则仅提供URI就无法减小图像的大小。 So here is the solution : 所以这是解决方案:

I have modified the simpleAdapter to be able to handle bitmap. 我修改了simpleAdapter使其能够处理位图。 Add this class into your project, and use it instead of simpleAdapter. 添加这个类到你的项目,并用它来代替simpleAdapter。 Then instead of passing an URI or a ressourceId for an image, pass a Bitmap ! 然后,不传递图像的URI或ressourceId,而是传递Bitmap!

Hereunder is the code : 以下是代码:

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



}

This class behave exactly like the original class (SimpleAdapter) 此类的行为与原始类(SimpleAdapter)完全相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM