How to display picture from web in ListActivity Customer item with SimpleAdapter?
Asked Answered
F

2

2

I Create a Activity Extends ListActivity I can used SimpleAdapter to display R.drawable.picture but I want to display picture from web in CustomerItem How do?

List data; List> plan_list;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.plan_list);
    plan_list = getPlanList();
    Plan_SimpleAdapter adapter = new Plan_SimpleAdapter(this, plan_list,
            R.layout.plan_item, new String[] { "Title", "miaoshu","img" }, new int[] {
                    R.id.textView1,R.id.miaoshu, R.id.plan_logo });
    setListAdapter(adapter);
}

// return plan list
private List<Map<String, Object>> getPlanList() {
    plan_list = new ArrayList<Map<String, Object>>(3);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("Title", "Android");
    map.put("miaoshu","M");
    map.put("img", "http://www.deakin.edu.au/conferences/icpads2008/images/deakin_logo.png");
    plan_list.add(map);

    map = new HashMap<String, Object>();
    map.put("Title", "Lenovo");
    map.put("miaoshu", "N");
    map.put("img", R.drawable.planlogo);
    plan_list.add(map);

    map = new HashMap<String, Object>();
    map.put("Title", "Droid");
    map.put("miaoshu", "O");
    map.put("img", R.drawable.planlogo);
    plan_list.add(map);
    return plan_list;
}

public class Plan_SimpleAdapter extends SimpleAdapter {

public Plan_SimpleAdapter(Context context,
        List<? extends Map<String, ?>> data, int resource, String[] from,
        int[] to) {
    super(context, data, resource, from, to);
    // TODO Auto-generated constructor stub
}

@Override
public void setViewImage(ImageView v, String value) {
    // TODO Auto-generated method stub
    try {
        URL url = new URL(value);
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        Bitmap bm = BitmapFactory.decodeStream(is);
        v.setImageBitmap(bm);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    super.setViewImage(v, value);
}

}

Fuel answered 4/3, 2011 at 9:49 Comment(1)
HI! Did you get this work? Can you please share your solution? ThanksPsychomotor
V
4

you can override following method in SimpleAdapter:

    @Override
    public void setViewImage(ImageView v, String value) {
        super.setViewImage(v, value);
        URL url;
        try {
            url = new URL(value);
            URLConnection conn = url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            Bitmap bm = BitmapFactory.decodeStream(is);
            v.setImageBitmap(bm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

the parameter "String value" is the url in your dataset.

Versed answered 4/3, 2011 at 10:15 Comment(1)
This doesn't work now because it's trying to load the image on the Main Thread.Duplicity
I
0

Override the setViewImage in SimpleAdapter and load the image url, in optimized way.

@Override
public void setViewImage(final ImageView v, final String value) {
    new AsyncTask<Void, Integer, InputStream>() {
        @Override
        protected InputStream doInBackground(Void... params) {
            try {
                return new URL(value).openConnection().getInputStream();

            } catch (Exception ex) {
                // handle the exception here
            }
            return null;
        }

        @Override
        protected void onPostExecute(InputStream result) {
            if(result != null) {
                Bitmap bitmap = BitmapFactory.decodeStream(result);
                v.setImageBitmap(bitmap);
            }
        }
    }.execute();        
}
Infighting answered 4/5, 2014 at 3:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.