2

以下は私のコードです。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が1つあります。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);
4

2 に答える 2

2

基本的に、単純なアダプタは、リソースIDまたはURIを行レイアウトのimageviewに自動的にバインドします。ただし、ビットマップはサポートされていません。

これは問題です。ビットマップを管理しなければならなかったすべての人が、outOfMemory例外を防ぐために画像のサイズを縮小しなければならないことがよくあることを知っているからです。ただし、画像をlistViewに追加する場合、URIのみを指定すると、画像のサイズを縮小することはできません。だからここに解決策があります:

simpleAdapterを変更して、ビットマップを処理できるようにしました。このクラスをプロジェクトに追加し、simpleAdapterの代わりに使用します。次に、画像のURIまたはressourceIdを渡す代わりに、ビットマップを渡します。

以下はコードです:

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)とまったく同じように動作します

于 2012-11-06T09:18:23.903 に答える
0

これがリストビュー内に画像を配置する方法です

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をご覧ください。

于 2011-05-25T12:27:49.003 に答える