2012-06-06 50 views
1

我顯示的聯繫人列表。除了該設備有1620個聯繫人,所以列表滾動非常緩慢甚至有時掛起。適配器的getView方法中的毛刺

 I tried using a check in getView method for ConvertView!=null but it alwayz inflate same view many times. thanks in advance.. 

     My code for getView method:- 
     if(ConvertView==null) 
       { view= mInflater.inflate(R.layout.facebookfriend, null); 
         TextView name=(TextView)view.findViewById(R.id.textView1); 
         ImageView image=(ImageView)view.findViewById(R.id.imageView1); 
        name.setText(mlist.get(position).get("name")); 

         String Id=mlist.get(position).get("contactId"); 
         Log.e("Id",""+Id); 
         CheckBox chkbox= (CheckBox)view.findViewById(R.id.checkBox1); 
         chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
          @Override 
          public void onCheckedChanged(
            CompoundButton buttonView, boolean isChecked) { 
           isSelected.set(position, isChecked); 
          } 
         }); 

             String photoid=mlist.get(position).get("photoId"); 
        Log.e("photoid",""+photoid); 
         if(mlist.get(position).get("photoId")!=null){ 
          Log.e("photoid",""+"photoid"); 
          image.setImageBitmap(loadContactPhoto(Id, mlist.get(position).get("photoId"))); 
        } 
       } 
      } 

有人告訴我讓listview根據需要動態加載數據,也使用ViewHolder使其更高效。 我已經試過它使用視圖持有人,但它每次膨脹單一視圖一次又一次。我的列表滾動非常順利,但真的不知道爲什麼它一次又一次地膨脹相同的看法。請幫忙 。 我與viewHolder代碼: -

    if (ConvertView == null) { 
         holder = new ViewHolder(); 
         ConvertView= mInflater.inflate(R.layout.facebookfriend, null); 
         holder.name = (TextView)ConvertView.findViewById(R.id.textView1); 
         holder.imageView = (ImageView)ConvertView.findViewById(R.id.imageView1); 
         holder.chkbox= (CheckBox)ConvertView.findViewById(R.id.checkBox1); 
         ConvertView.setTag(holder); 
        } else { 
         holder = (ViewHolder) ConvertView.getTag(); 
        } 
        holder.name.setText(mlist.get(position).get("Name").toString()); 

        holder. chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
         @Override 
         public void onCheckedChanged(
           CompoundButton buttonView, boolean isChecked) { 
          isSelected.set(position, isChecked); 
          Log.e("position"+position,""+isChecked); 
         } 
        }); 
        String Id=mlist.get(position).get("Contactid").toString(); 

        Log.e("Id","--------------"+Id); 
        if(mlist.get(position).get("Photoid")!=null){ 
         Log.e("photoid",""+"photoid"); 
         holder.imageView.setImageBitmap(loadContactPhoto(Id, mlist.get(position).get("Photoid"))); 
         Log.e("position position",""+position); 
        } 
       } 

    static class ViewHolder { 
     TextView name; 
     ImageView imageView; 
     CheckBox chkbox; 
    } 

我對Imagefrom接觸方法的代碼,我該死的肯定此代碼

private Bitmap loadContactPhoto(String id, String photoId) { 
     Long _id=Long.parseLong(id); 
     Long _photoId=0l; 
     if(photoId!=null){ 
      _photoId=Long.parseLong(photoId); 
     } 
     ContentResolver cr = Setting.this.getContentResolver(); 
     Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, _id); 
     InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); 
     if (input != null) { 
      return BitmapFactory.decodeStream(input); 
     } else { 
      Log.d("PHOTO", "first try failed to load photo"); 

     } 

     byte[] photoBytes = null; 
     Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, _photoId); 
     Cursor c = cr.query(photoUri,new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO }, 
       null, null, null); 
     try { 
      if (c.moveToFirst()) 
       photoBytes = c.getBlob(0); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      c.close(); 
     } 

     if (photoBytes != null) 
      return BitmapFactory.decodeByteArray(photoBytes, 0, 
        photoBytes.length); 
     else 
      Log.d("PHOTO", "second try also failed"); 
     Bitmap icon = BitmapFactory.decodeResource(this.getResources(), 
       R.drawable.default_image); 
     return icon; 

}

+0

發佈您的'loadContactPhoto()'方法。 – Shaiful

+0

私人位圖loadContactPhoto(String id,String photoId){ \t \t Long _id = Long.parseLong(id); \t \t Long _photoId = 0l; (photoId!=空){ \t \t \t _photoId = Long.parseLong(photoId); \t \t} \t \t ContentResolver cr = Setting.this.getContentResolver(); \t \t Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,_id); \t \t InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr,uri); \t \t if(input!= null){ \t \t \t return BitmapFactory.decodeStream(input); \t \t} else { \t \t \t Log.d(「PHOTO」,「第一次嘗試未能加載照片」); \t \t} – LuminiousAndroid

+0

byte [] photoBytes = null; \t \t Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI,_photoId); \t \t光標c = cr.query(photoUri,new String [] {ContactsContract.CommonDataKinds.Photo。PHOTO}, \t \t \t \t null,null,null); \t \t嘗試{ \t \t \t如果(c.moveToFirst()) \t \t \t \t photoBytes = c.getBlob(0); (例外e){ \t \t} catch(Exception e){ \t \t \t e.printStackTrace(); \t \t} finally { \t \t \t c.close(); \t \t} – LuminiousAndroid

回答

1

你的問題是loadContactPhoto()我想。每次使用loadContactPhoto()您正在運行查詢並通過解碼數組來獲取位圖。這就是滾動速度變慢的原因。

嘗試加載ListView之前加載所有聯繫人圖像,並讓我們知道發生了什麼。

您也可以按照本教程使用SimpleCursorAdapter加載與照片的聯繫人。

Add contact photo to your list application

+0

當我們每次滾動getView方法調用並膨脹視圖時,我都使用ViewHolder。沒有什麼值得關注的loadcontactimage方法。 – LuminiousAndroid

+0

先試試這種方法。你如何確定loadContactImage工作正常? – Shaiful

+0

因爲我的問題不是緩慢滾動,我的問題是每次我的單一視圖膨脹爲每個視圖... – LuminiousAndroid