简体   繁体   中英

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

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

}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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