Note that there are some explanatory texts on larger screens.

plurals
  1. POANDROID : Load asynchronous pictures in listView
    primarykey
    data
    text
    <p>I 'd like to display a ListView with a customized adapter (with picture and text).</p> <p>Images are loaded from distant servers, so I have decided to use AsyncTask.</p> <p>Actually, pictures are well displayed but if I scroll down quickly, a wrong picture is displayed during 1/2 secondes (after loading, correct picture appears)</p> <p>Here is my adapter's code:</p> <pre><code>public class GiAdapter extends BaseAdapter { private Context mContext; private List&lt;SiteStaff&gt; mListAppInfo; private HashMap&lt;Integer, ImageView&gt; views; private HashMap&lt;String,Bitmap&gt; oldPicts = new HashMap&lt;String,Bitmap&gt;(); private LayoutInflater mInflater; private boolean auto; private final String BUNDLE_URL = "url"; private final String BUNDLE_BM = "bm"; private final String BUNDLE_POS = "pos"; private final String BUNDLE_ID = "id"; public GiAdapter(Context context, List&lt;SiteStaff&gt; list) { mContext = context; mListAppInfo = list; views = new HashMap&lt;Integer, ImageView&gt;(); mInflater = LayoutInflater.from(mContext); } @Override public int getCount() { return mListAppInfo.size(); } @Override public Object getItem(int position) { return mListAppInfo.get(position).getId(); } @Override public long getItemId(int position) { return mListAppInfo.get(position).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout layoutItem; // reuse of convertView if (convertView == null) { layoutItem = (LinearLayout) mInflater.inflate(R.layout.auto_gi, parent, false); } else { layoutItem = (LinearLayout) convertView; } // infos for the current element SiteStaff entry = mListAppInfo.get(position); //set some text fields TextView name = (TextView) layoutItem.findViewById(R.id.name); TextView size = (TextView) layoutItem.findViewById(R.id.size); name.setText(entry.getName()); size.setText(entry.getSize()); // get the imageView for the current object ImageView v = (ImageView) layoutItem.findViewById(R.id.gi_image); // put infos in bundle and send to the LoadImage class Bundle b = new Bundle(); //url of the pict b.putString(BUNDLE_URL, entry.getUrl()); //position in the listView b.putInt(BUNDLE_POS, position); //id of the current object b.putInt(BUNDLE_ID, entry.getId()); //put info in the map in order to display in the onPostExecute views.put(position, v); // thread new LoadImage().execute(b); return layoutItem; } //asyncTackClass for loadingpictures private class LoadImage extends AsyncTask&lt;Bundle, Void, Bundle&gt; { @Override protected Bundle doInBackground(Bundle... b) { Bitmap bm =null; //cache: for better performance, check if url alredy exists if(oldPicts.get(b[0].getString(BUNDLE_URL))==null){ bm = Utils.getBitMapFromUrl(b[0].getString(BUNDLE_URL)); oldPicts.put(b[0].getString(BUNDLE_URL),bm); }else{ bm = oldPicts.get(b[0].getString(BUNDLE_URL)); } // get info from bundle Bundle bundle = new Bundle(); bundle.putParcelable(BUNDLE_BM, bm); bundle.putInt(BUNDLE_POS, b[0].getInt(BUNDLE_POS)); return bundle; } @Override protected void onPostExecute(Bundle result) { super.onPostExecute(result); //get picture saved in the map + set ImageView view = views.get(result.getInt(BUNDLE_POS)); Bitmap bm = (Bitmap) result.getParcelable(BUNDLE_BM); if (bm != null){ //if bitmap exists... view.setImageBitmap(bm); }else{ //if not picture, display the default ressource view.setImageResource(R.drawable.unknow); } } } } </code></pre> <p>Thank you !</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.
 

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