Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What I have now is the following:</p> <p>ImageAdapter has <code>public static ArrayList&lt;Bitmap&gt; bitmapArray = new ArrayList&lt;Bitmap&gt;();</code></p> <p>And the getView is this: </p> <pre><code>public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); LoadThumbs downloader = new LoadThumbs(imageView); downloader.execute(Loggedin.mImageIds.get(position)); } else { imageView = (ImageView) convertView; try{ imageView.setImageBitmap(bitmapArray.get(position+1)); } catch (Exception e){} } return imageView; } </code></pre> <p>And then in LoadThumbs:</p> <pre><code>protected void onPostExecute(Bitmap result) { if (imageViewReference != null) { ImageView imageView = imageViewReference.get(); if (imageView != null) { ImageAdapter.bitmapArray.add(result); imageView.setImageBitmap(result); } } } </code></pre> <p>What this does is it first downloads the images, puts them in the imageView. At the same time it puts them in the Bitmap array. When the screen rotates, it uses the images in the array instead of redownloading them. As far as I understand.</p> <p>It uses <code>imageView.setImageBitmap(bitmapArray.get(position+1));</code> (Note the +1) because for some reason the second bitmap (index 1) in the bitmapArray is a duplicate of the first (index 0).</p> <p>This now works. I bet it's not the best or cleanest way of doing this, so if any of you have improvements for my code, let it know!</p> <h2>EDIT</h2> <p>For anyone trying to solve this. I ended up with a solution that now 100% works for me.</p> <p>I first filled the bitmap ArrayList with empty indexes:</p> <pre><code> for (int i = 0; i &lt; Loggedin.mImageIds.size(); i++) { ImageAdapter.bitmapArray.add(null); } </code></pre> <p>I then made a new Object to pass to AsyncTask instead of a string. This object holds the url and the position integer.</p> <pre><code>public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); LoadThumbs downloader = new LoadThumbs(imageView); Object[] arr = new Object[2]; arr[0] = Loggedin.mImageIds.get(position); arr[1] = new Integer(position); downloader.execute(arr); } else { imageView = (ImageView) convertView; try { imageView.setImageBitmap(bitmapArray.get(position)); } catch(Exception e){} } return imageView; } </code></pre> <p>In LoadThumbs I have an Object instead of the string</p> <pre><code>public class LoadThumbs extends AsyncTask&lt;Object, Void, Bitmap&gt; { </code></pre> <p>Then in the <code>doInBackground</code> method I get the correct parameters from the object.</p> <pre><code>url = (String) params[0]; position = (Integer) params[1]; </code></pre> <p>In <code>onPostExecute</code> method I put the image in the array at the correct position:</p> <pre><code>try { ImageAdapter.bitmapArray.set(position, result); } catch (Exception e) {} </code></pre> <p>At last now the <code>else</code> branch in the getView method can access the correct bitmaps.</p>
 

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