3

我正在創建一個自定義聯繫人應用程序..我使用ViewHolder的ArrayAdapter設計模式進行優化...由於加載縮略圖圖片需要很多時間,使用AsyncTask類來加載圖像,對於我的屏幕中的第一組聯繫人,圖片加載得很好,但當我向下滾動時,同一組圖片隨機重新循環其他聯繫人...我已經搜索過forumn,並找到了一些解決方案,但沒有爲我工作。我將我的代碼如下..有人請告訴我什麼是錯的代碼,而不是將其標記爲複製QN ...Android-使用ViewHolder和AsyncTask加載聯繫人縮略圖問題

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

我在這裏沒有看到問題。您應該可以將holder.imageView.setImageBitmap調用到您最初在getView方法中創建持有者時的原始內容。否則,圖像仍然具有舊值,直到加載完成。 –

+0

感謝或你的迴應...但我真的沒有得到你的改變...你可以說明它或突出顯示應該改變的代碼? –

+1

在開始加載AsyncTask之前,您應該設置holder.imageView.setImageBitmap(loadingImage),否則舊圖像仍會顯示,因爲ListView會循環它的視圖 - 在頂部滾動的那個會重新插入到底部(和其他方式)。 –

回答

3

想出來了......現在它的工作...... Simon Meyer給出的評論是正確的..我需要在我可以調用Async Task之前將imageView初始化爲位圖。這裏是代碼變化

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

我認爲你的自定義類沒有找到lader圖像類的上下文。 首先在你的活動中聲明上下文。所有這一切都很順利,而不是這個......它的工作很好

+0

但是,先生們這些類不在我的活動中......我把它們作爲單獨的類...所以getContext()不起作用,tats爲什麼我已經將上下文作爲Async類的參數傳遞......是否意味着它是問題?如果是的話如何獲得上下文? –