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

前端 未结 2 1905
轻奢々
轻奢々 2021-01-15 03:49

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

相关标签:
2条回答
  • 2021-01-15 04:43

    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.

    0 讨论(0)
  • 2021-01-15 04:43

    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();        
    }
    
    0 讨论(0)
提交回复
热议问题