Note that there are some explanatory texts on larger screens.

plurals
  1. POLazy load of images on ListView with AsyncTask launched on the getView (adapter)
    primarykey
    data
    text
    <p>I have a ListView with items that have some text and one ImageView. I would like to use and AsyncTask in order to load the ImageView (lazy load). I know there are some libraries in order to do that. The problem is that is not possible to get the images just with a URL, like</p> <pre><code>imageLoader.displayImage(imageUri, imageView); </code></pre> <p>because I need to add some headers to the HTTP GET (authentication token). So inside the getView() of the adapter I have to launch an AsyncTask that will download the image as Bitmap and show it on the ImageView.</p> <p>Is there any code example? I tried to pass the ImageView to the AsyncTask in the constructor of the AsyncTask, download the Bitmap and assign it to the ImageView on the onPostExecute, something like this:</p> <pre><code>public class LoadImageTask extends AsyncTask&lt;String, Void, Bitmap&gt;{ private ImageView mImage; private Context mContext; private String imageId; private Throwable mThrown = null; public LoadImageTask(ImageView ivImage, Context context) { mImage = ivImage; mContext = context; } @Override protected Bitmap doInBackground(String... params) { imageId = params[0]; // Build request and request token .... //Process the response try { HttpEntity entity = mHttpResponse.getEntity(); InputStream inputStream = entity.getContent(); BufferedInputStream bis = new BufferedInputStream(inputStream); Bitmap bm = BitmapFactory.decodeStream(bis); bis.close(); inputStream.close(); return bm; } catch (IOException e) { mThrown = e; RestClient.consumeContent(mHttpResponse); return null; } } RestClient.consumeContent(mHttpResponse); return null; } @Override protected void onPostExecute(Bitmap bm) { mImage.setImageBitmap(bm); } </code></pre> <p>}</p> <pre><code>public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.news_item, null); TextView tvNewsTitle = (TextView) rowView.findViewById(R.id.tvNewsTitle); TextView tvUpdateDate = (TextView) rowView.findViewById(R.id.tvUpdateDate); ImageView ivImage = (ImageView) rowView.findViewById(R.id.ivNewsThumbnail); News news = mNewsList.get(position); tvNewsTitle.setText(news.getTitle()); tvUpdateDate.setText(news.getLastUpdate()); new LoadImageTask(ivImage, mContext).execute(news.getImageThumbnail()); return rowView; } </code></pre> <p>But looks like it's always updating only the fist element of the ListView. </p> <p>Is there any example code?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload