加载通讯录与ViewHolder和AsyncTask

加载通讯录与ViewHolder和AsyncTask

本文介绍了Android-加载通讯录与ViewHolder和AsyncTask-缩略图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个自定义联系人应用程序..我用ViewHolder设计模式ArrayAdapter优化......既然花了很多时间来加载缩略图图片,我使用的加载图像的AsyncTask类,第一套在我的屏幕接触时,图片加载很好,但是当我向下滚动,相同的一组照片被随意用于其他联系人重新循环......我已搜查这个forumn,发现了几个解决方案,但没有为我工作。我现在就把我下面的code ..有人请告诉我什么是错在code,而不是将其标记为正,副本QN ...

 公共类CustomList扩展ArrayAdapter<串GT; {    私人最终活动范围内;
    私人最终的ArrayList<串GT;显示名,demoteValue,突发事件;
    ArrayList的<龙>的ContactID;    公共CustomList(Activity上下文,ArrayList的<龙>的ContactID,ArrayList的<串GT;显示名,ArrayList的<串GT; demoteValue,
            ArrayList的<串GT;应急){
        超(背景下,R.layout.list_single,显示名);
        this.context =背景;
        this.displayName =显示名;
        this.demoteValue = demoteValue;
        this.emergency =紧急情况;
        this.contactId =的ContactID;    }    类MyViewHolder
    {
        公共QuickContactBadge ImageView的;
        公众的TextView txtTitle;
        INT位置;        MyViewHolder(视图V){
            txtTitle =(TextView中)v.findViewById(R.id.txt);
            ImageView的=(QuickContactBadge)v.findViewById(R.id.contactimageentry);
        }
    }
    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看rowView = convertView;
        MyViewHolder支架=无效;        如果(rowView == NULL)
        {
             LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             rowView = inflater.inflate(R.layout.list_single,父母,假);
             持有人=新MyViewHolder(rowView);
             rowView.setTag(保持器);        }其他{
            支架=(MyViewHolder)rowView.getTag();
        }        holder.txtTitle.setText(displayName.get(位置));
        holder.txtTitle.setPadding(5,5,5,5);
        //有些唧唧歪歪的工作。
        如果(emergency.get(位置).equals(1)){            holder.txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,25);
            。holder.imageView.getLayoutParams()宽度=的getPixels(TypedValue.COMPLEX_UNIT_DIP,50);}
        //获取和设置光这里谎言到问题的CALL
        holder.position =位置;
        新LoadContactImage(的getContext(),持有人,位置,contactId.get(位置))
        .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,NULL);
        返回rowView;
    }
}

这是我的任务异步的code加载基于ID,同时调用它,我已经通过了图像

 公共类LoadContactImage扩展的AsyncTask<对象,太虚,位图> {私人长期路径;
私人MyViewHolder mHolder;
私人诠释mposition;
上下文CTX;公共LoadContactImage(上下文的背景下,MyViewHolder架,INT位置,长ID){
    this.mHolder =持有人;
    this.Path = ID;
    this.mposition =位置;
    this.ctx =背景;
}@覆盖
保护位图doInBackground(对象... PARAMS){
    乌里my_uri = getPhotoUri(路径);
    ContentResolver的CR = ctx.getContentResolver();
    在的InputStream = NULL;
    尝试{
        在= cr.openInputStream(my_uri);
    }赶上(FileNotFoundException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    位图位图= BitmapFactory.de codeStream(中);
    返回位图;
}@覆盖
保护无效onPostExecute(位图结果){    super.onPostExecute(结果);
    如果(结果=空&安培;!&安培; mposition == mHolder.position){
        //imv.setImageBitmap(result);
        mHolder.imageView.setImageBitmap(结果);
    }
}
}


解决方案

想通了......现在,它在评论由西蒙·迈尔给working..Soln是correct..I必要的初始化的ImageView在getView位图()之前,我可以调用异步Task..Here是code修改

  holder.position =位置;holder.imageView.setImageURI(Uri.parse(android.resource://com.vishnu.demotingprototype/drawable/ic_contact_picture));新LoadContactImage(的getContext(),持有人,位置,contactId.get(位置))执行();

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);
    }
}
}
解决方案

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

这篇关于Android-加载通讯录与ViewHolder和AsyncTask-缩略图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:56