2012-12-18 18 views
1

我有一個arrayaadapter,我正在檢索圖像 的手機通訊錄號碼並將其顯示在列表中。Android:停止在列表視圖中重複圖像,性能問題

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    View view = convertView; 

    if (view == null) { 
    LayoutInflater inflater = (LayoutInflater) (getContext() 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 
    view = inflater.inflate(renderer, null); 
    } 


    TextView text = (TextView) view.findViewById(R.id.name); 
    TextView textContNo = (TextView) view.findViewById(R.id.contactno); 
    TextView textEmailId = (TextView) view.findViewById(R.id.emailId); 
    Profile contact = listCont.get(position); 
    text.setText(contact.getName());  

    QuickContactBadge photo = (QuickContactBadge) view.findViewById(R.id.quickContactBadge1); 
    photo.setTag(contact.getMobileNo()); 
    new LoadImage(photo).execute(contact.getMobileNo()); 

和加載使用的AsyncTask

class LoadImage extends AsyncTask<String, Void, Bitmap>{ 

     private QuickContactBadge qcb; 

     public LoadImage(QuickContactBadge qcb) { 
     this.qcb= qcb; 
     } 
     @Override 
     protected Bitmap doInBackground(final String... params) { 
     activity.runOnUiThread(new Runnable() { 
     public void run() { 
     new QuickContactHelper(activity, qcb, (String) params[0]).addThumbnail(); 
     } 
     }); 
     return null; 
     } 
     @Override 
     protected void onPostExecute(Bitmap result) { 

     } 
     } 

我面臨兩個問題在backgroundthread圖像,圖像被重複,而滾動卻並不順利 IAM試圖getview方法implemente viewholder,但不知道如何使用它或有任何其他方式來停止圖像重複。任何幫助表示讚賞

+0

您目前的代碼正在工作?如果是的話,你會得到任何錯誤,然後發佈日誌 –

+0

是的,我的正確代碼工作..沒有得到錯誤,但圖像重複和滾動is struckin – teekib

+0

我有問題在這裏爲什麼你使用AsyncTask,因爲你在doInBackground然後使用runOnUiThread代碼總是在用戶界面上執行線程 –

回答

0

要加載圖像有效,Universal Image Loader是一個很好的庫加載在後臺高效圖像(懶惰加載)。

有些時候會發生什麼,當你第一次膨脹任何視圖時,滾動視圖,列表有重複舊視圖的一些限制。做最好的解決辦法是,你必須使用ViewHolder持有你所有的觀點一樣

class ViewHolder 
{ 
    View rowView; 
    TextView textview = null; 

    public ViewHolder(View view) 
    { 
      rowView = view; 
    } 

    public TextView getTextView() 
    { 
      if(textview ==null) 
       textview = rowView.findViewById(R.id.text1); 
      return textview; 
    } 
} 

,你可以在你的適配器一樣使用它,

if(convertview ==null) 
{ 
    convertview = inflater.inflate(renderer, null); 
    ViewHolder holder = new ViewHolder(convertview); 
    convertview.setTag(holder); 
} 

ViewHolder tempHolder = (ViewHolder) convertview.getTag(); 

TextView textView = tempHolder.getTextView(); 

如果你做這個事情,你的參考textview和其他意見將被保留在ViewHolder

+0

所以我應該讓我所有的視圖都是視圖類,後來稱之爲 – teekib

+1

holder =新的ViewHolder(view); holder.text =(TextView)view.findViewById(R.id.name); holder.textContNo =(TextView)view.findViewById(R.id.contactno); holder.textEmailId = (TextView)view.findViewById(R.id.emailId); view.setTag(保持器);其他{ holder =(ViewHolder)view.getTag(); } private static class ViewHolder { public TextView textEmailId; \t \t public TextView textContNo; \t \t public TextView text; \t \t } – teekib

+0

是的,這會讓您創建一個適配器的良好實踐。 –