Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid- Loading Contacts with ViewHolder and AsyncTask- Thumbnail issue
    text
    copied!<p>I am creating a custom contact app.. I use a ArrayAdapter with ViewHolder design pattern for optimization...Since it took a lot of time to load the thumbnail pics, I use AsyncTask class for loading images, for the first set of contacts in my screen, the pics are loading well but when I scroll down, the same set of pictures are being re-cycled at random for other contacts...I have searched this forumn and found a few solutions but none worked for me. I shall put my code below.. Someone pls tell me what is wrong in the code instead of marking it as duplicate qn...</p> <pre><code>public class CustomList extends ArrayAdapter&lt;String&gt;{ private final Activity context; private final ArrayList&lt;String&gt; displayName,demoteValue,emergency; ArrayList&lt;Long&gt; contactId; public CustomList(Activity context,ArrayList&lt;Long&gt; contactId,ArrayList&lt;String&gt; displayName,ArrayList&lt;String&gt; demoteValue , ArrayList&lt;String&gt; emergency) { super(context, R.layout.list_single, displayName); this.context = context; this.displayName = displayName; this.demoteValue = demoteValue; this.emergency = emergency; this.contactId=contactId; } class MyViewHolder { public QuickContactBadge imageView; public TextView txtTitle; int position; MyViewHolder(View v){ txtTitle = (TextView) v.findViewById(R.id.txt); imageView = (QuickContactBadge) v.findViewById(R.id.contactimageentry); } } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView=convertView; MyViewHolder holder=null; if(rowView==null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); rowView = inflater.inflate(R.layout.list_single, parent, false); holder=new MyViewHolder(rowView); rowView.setTag(holder); }else{ holder= (MyViewHolder) rowView.getTag(); } holder.txtTitle.setText(displayName.get(position)); holder.txtTitle.setPadding(5, 5, 5, 5); //Some BLA BLA BLA work. if(emergency.get(position).equals("1")){ holder.txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,25); holder.imageView.getLayoutParams().width=getPixels(TypedValue.COMPLEX_UNIT_DIP,50); } //Get and set the photo-HERE LIES THE CALL TO THE PROBLEM holder.position = position; new LoadContactImage(getContext(),holder,position,contactId.get(position)) .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null); return rowView; } } </code></pre> <p>And here is the code for my Async Task that loads the images based on the ID which I have passed while calling it</p> <pre><code>public class LoadContactImage extends AsyncTask&lt;Object, Void, Bitmap&gt; { private long Path; private MyViewHolder mHolder; private int mposition; Context ctx; public LoadContactImage(Context context,MyViewHolder holder,int position,Long id) { this.mHolder= holder; this.Path = id; this.mposition=position; this.ctx= context; } @Override protected Bitmap doInBackground(Object... params) { Uri my_uri = getPhotoUri(Path); ContentResolver cr = ctx.getContentResolver(); InputStream in = null; try { in = cr.openInputStream(my_uri); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Bitmap bitmap = BitmapFactory.decodeStream(in); return bitmap; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if (result != null &amp;&amp; mposition == mHolder.position) { //imv.setImageBitmap(result); mHolder.imageView.setImageBitmap(result); } } } </code></pre>
 

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