如何从哈希表获取位图图像到简单的适配器中以在listactivity的imageview中查看

发布于 2024-11-09 08:12:15 字数 2366 浏览 0 评论 0原文

下面是我的代码。我通过 JSONArray 获取图像的 url。然后我将图像转换为位图图像并存储在哈希表中。

我正在使用 ImageView 在列表视图中显示图像。所以我使用简单的适配器来获取图像。但图像未加载。

请帮助我解决这个问题,我是 Android 新手!!!!!!!

ArrayList<Hashtable<String, Bitmap>> mylist1 = new ArrayList<Hashtable<String, Bitmap>>();

try{                
    JSONArray  earthquakes = json.getJSONArray("earthquakes");              
    for(int i=0;i<10;i++){                      
        imagemap = new Hashtable<String, Bitmap>();
        JSONObject e = earthquakes.getJSONObject(i);
        try
        {
            aURL = new URL(photo);
        }
        catch (MalformedURLException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        URLConnection conn = null;
        try
        {
            conn = aURL.openConnection();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try
        {
            conn.connect();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        InputStream is = null;
        try
        {
            is = conn.getInputStream();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        BufferedInputStream bis = new

        BufferedInputStream(is,8*1024);

        Bitmap bm = BitmapFactory.decodeStream(bis);

        imagemap.put("im",bm);

        mylist1.add(imagemap);
        ListAdapter adapter = new SimpleAdapter(this,mylist1,R.layout.main,new String[] {"im"},new int[] {R.id.image});

        setListAdapter(adapter);

更新:

下面是我用来将字符串加载到列表视图中的文本字段的ArrayAdapter。 我里面有一个 ImageView 。如何重写 listadapter 将动态图像加载到列表视图中。

ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main4, 
                       new String[] { "percent","propertyid",  "cityname", "statecode", "propertytype", "footage", "bathroom", "bedroom", "price", "estimated" }, 
                        new int[] { R.id.percent, R.id.property_id,  R.id.city_name, R.id.state_code, R.id.prop_type, R.id.foot, R.id.bath, R.id.bed, R.id.list, R.id.estimat});

setListAdapter(adapter);

Below is my code. I am getting url for an image through JSONArray. Then i converted the image to Bitmap image and stored in Hashtable.

I am using ImageView to show the images in listview. So i am using simple adapter for getting images. But image is not loading.

Please help me out of this, I am new to Android!!!!!!

ArrayList<Hashtable<String, Bitmap>> mylist1 = new ArrayList<Hashtable<String, Bitmap>>();

try{                
    JSONArray  earthquakes = json.getJSONArray("earthquakes");              
    for(int i=0;i<10;i++){                      
        imagemap = new Hashtable<String, Bitmap>();
        JSONObject e = earthquakes.getJSONObject(i);
        try
        {
            aURL = new URL(photo);
        }
        catch (MalformedURLException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        URLConnection conn = null;
        try
        {
            conn = aURL.openConnection();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try
        {
            conn.connect();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        InputStream is = null;
        try
        {
            is = conn.getInputStream();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        BufferedInputStream bis = new

        BufferedInputStream(is,8*1024);

        Bitmap bm = BitmapFactory.decodeStream(bis);

        imagemap.put("im",bm);

        mylist1.add(imagemap);
        ListAdapter adapter = new SimpleAdapter(this,mylist1,R.layout.main,new String[] {"im"},new int[] {R.id.image});

        setListAdapter(adapter);

Update:

Below is the ArrayAdapter i used to load the string to text fields in the listview.
I have one ImageView in it. How to override the listadapter to load the dynamic images into the listview.

ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main4, 
                       new String[] { "percent","propertyid",  "cityname", "statecode", "propertytype", "footage", "bathroom", "bedroom", "price", "estimated" }, 
                        new int[] { R.id.percent, R.id.property_id,  R.id.city_name, R.id.state_code, R.id.prop_type, R.id.foot, R.id.bath, R.id.bed, R.id.list, R.id.estimat});

setListAdapter(adapter);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期以后 2024-11-16 08:12:15

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

这是一个问题,因为每个必须管理位图的人都知道,我们经常必须减小图片的大小以防止内存不足异常。
但是,如果您想将图像添加到 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<? extends Map<String, ?>> map; // if fails to compile, replace with List<HashMap<String, Object>> map
    String[] from;
    int layout;
    int[] to;
    Context context;
    LayoutInflater mInflater;
    public ExtendedSimpleAdapter(Context context, List<? extends Map<String, ?>> data, // if fails to compile, do the same replacement as above on this line
            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)完全相同

Basically the simple adapter automatically bind some ressource id or URI to the imageview of your row layout.
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. So here is the solution :

I have modified the simpleAdapter to be able to handle bitmap.
Add this class into your project, and use it instead of simpleAdapter.
Then instead of passing an URI or a ressourceId for an image, pass a 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<? extends Map<String, ?>> map; // if fails to compile, replace with List<HashMap<String, Object>> map
    String[] from;
    int layout;
    int[] to;
    Context context;
    LayoutInflater mInflater;
    public ExtendedSimpleAdapter(Context context, List<? extends Map<String, ?>> data, // if fails to compile, do the same replacement as above on this line
            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)

心不设防 2024-11-16 08:12:15

这就是我将图像放入列表视图中的方法

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.highscore_item, null);
        holder=new ViewHolder();
        holder.Icon=(ImageView)vi.findViewById(R.id.icon);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();
    holder.Icon.setTag(mData[position].getIcon());
    imageLoader.DisplayImage(mData[position].getIcon(), activity, holder.Icon);
    return vi;
}

看看 Lazylist 如果您想了解更多信息。

This is how I put an image inside a listview

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.highscore_item, null);
        holder=new ViewHolder();
        holder.Icon=(ImageView)vi.findViewById(R.id.icon);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();
    holder.Icon.setTag(mData[position].getIcon());
    imageLoader.DisplayImage(mData[position].getIcon(), activity, holder.Icon);
    return vi;
}

Take a look at Lazylist if you want more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文