Android-使用ViewHolder和AsyncTask加载联系人 - 缩略图问题

时间:2013-11-24 09:40:54

标签: android design-patterns android-asynctask thumbnails android-contacts

我正在创建一个自定义联系人应用程序..我使用带有ViewHolder设计模式的ArrayAdapter进行优化...由于需要花费大量时间来加载缩略图,我使用AsyncTask类来加载图像,第一组在我的屏幕上的联系人,图片正在加载,但当我向下滚动时,同一组图片正在为其他联系人随机重新循环...我搜索了这个forumn并找到了一些解决方案,但没有一个对我有效。我将把我的代码放在下面..有人请告诉我代码中的错误而不是将其标记为重复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;
    }   
}

这是我的异步任务的代码,它根据我在调用时传递的ID加载图像

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);
    }
}   
}

2 个答案:

答案 0 :(得分:3)

想出来......现在它的工作...... Simon Meyer在评论中给出的声音是正确的..我需要在调用异步任务之前将imageView初始化为getView()中的位图。这是改变在代码中

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();

答案 1 :(得分:0)

我认为你的自定义类没有找到lader图像类的上下文。 首先在yorr活动中声明上下文。所有进展顺利而不是......它的工作正常