2011-11-01 4 views
2

확장 SimpleCursorAdapter를 사용하여 연락처를 목록에로드했는데 이제 연락처 사진을로드하려고합니다. 코드를 실행할 때 임의의 연락처 사진은 사진이없는 사람들을 위해 임의의 연락처 옆에 나타납니다. 왜 그 사진을 가지고 사진 옆에있는 사람들에게 사진을 보내지 않는 것일까 요? 당신이해야하지연락처 목록에 연락처 사진로드 : 임의 연락처 사진이 잘못된 위치에 나타납니다.

ImageView photo = (ImageView) findViewById(R.id.photo); 

: loadContactPhoto에

public void bindView(View view, Context context, Cursor cursor) { 
ImageView photo = (ImageView) findViewById(R.id.photo); 
long photoId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID)); 

Bitmap photoBitmap = loadContactPhoto(photoId); 
    if (photoBitmap != null) { 
     photo.setImageBitmap(photoBitmap); 
    } 

그리고 코드 :

의 Bindview()에서
public Bitmap loadContactPhoto(long id) { 
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, id); 
    byte[] data = null; 
    Cursor cursor = managedQuery(
     contactUri, // Uri 
     new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO }, // projection, the contact photo 
     ContactsContract.Contacts.PHOTO_ID + "!= 0", // where statement, only if the contact has a photo 
     null, null); 
    Log.i(LOG_TAG, "cursorCount: " + cursor.getCount()); // returns 1 
    if (cursor == null || !cursor.moveToNext()) {   
     return null; 
    } 
    data = cursor.getBlob(0); 
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 
    return bitmap; 
} 
+0

"보기보기"를 어떻게 구현 했습니까? – SpicyWeenie

답변

0

, 당신이 전화하는거야 여기

코드입니다 뷰 매개 변수에서 findViewById()를 호출하고 있습니까?

ImageView photo = (ImageView) view.findViewById(R.id.photo); 
+0

예, 감사합니다! 이것은 부분적으로 문제를 해결했습니다. 이제 올바른 연락처 옆에 올바른 사진이 표시됩니다. 그러나 연락처 사진이없는 임의의 다른 연락처 옆에 여전히 임의의 사진이 있습니다. 어떤 아이디어? – Phat7

+0

명확히하기 위해 목록을 스크롤하기 시작할 때 이런 일이 발생합니다. 그것은 getView 또는 newView 때문에 내가 그 (것)들을 또는 너무 겹쳐서 써야 하는가 일 수 있 었는가? – Phat7

+0

또 다른 범인을 찾았습니다. 바인드 뷰는 행을 재활용하고 있었기 때문에 이전에로드 된 사진이 임의의 다른 행에 나타났습니다. if (photoBitmap! = null)에 else 문을 추가하여 수정. 도와 주셔서 감사합니다! – Phat7

관련 문제