问题
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...
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final ArrayList<String> displayName,demoteValue,emergency;
ArrayList<Long> contactId;
public CustomList(Activity context,ArrayList<Long> contactId,ArrayList<String> displayName,ArrayList<String> demoteValue ,
ArrayList<String> 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;
}
}
And here is the code for my Async Task that loads the images based on the ID which I have passed while calling it
public class LoadContactImage extends AsyncTask<Object, Void, Bitmap> {
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 && mposition == mHolder.position) {
//imv.setImageBitmap(result);
mHolder.imageView.setImageBitmap(result);
}
}
}
回答1:
Figured it out...Now its working..Soln given by Simon Meyer in Comments is correct..I need to initialize the imageView to a bitmap in getView() before I could call Async Task..Here is the change in code
holder.position = position;
holder.imageView.setImageURI( Uri.parse("android.resource://com.vishnu.demotingprototype/drawable/ic_contact_picture"));
new LoadContactImage(getContext(),holder,position,contactId.get(position)).execute();
回答2:
i think your customclass is not find out the context of lader image class. Declare context in yorr activity first.all is going well rather than this....Its works fine
来源:https://stackoverflow.com/questions/20173132/android-loading-contacts-with-viewholder-and-asynctask-thumbnail-issue