2014-09-03 12 views
0

그래서 나는 오래된 문제가있다. 아무도 그것이 가능한 방법을 짐작할 수 있습니까?이 SimpleCursorAdapter는 어떻게 작동합니까?

각 연락처에 대한 사진 미리보기 이미지와 함께 연락처 목록을 표시하고 있습니다. 반환 된 커서 데이터에는 사진 미리보기 이미지 데이터가 없지만 각 연락처에 대해 미리보기 이미지가 올바르게 표시됩니다.

목록 항목보기 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="horizontal" 
    > 

    <ImageView 
     android:id="@+id/userpic" 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:contentDescription="Userpic" 
     /> 

    <TextView 
     android:id="@android:id/text1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:textSize="24sp" 
     android:padding="12dp" 
     android:layout_gravity="center_vertical" 
     /> 
</LinearLayout> 

주요 코드 커서를 생성

private static class CursorAdapter extends SimpleCursorAdapter { 
    private static final String[] FROM = new String[]{ 
      Contacts.DISPLAY_NAME_PRIMARY, 
      Contacts.PHOTO_THUMBNAIL_URI 
    }; 
    private static final int[] TO = new int[]{ 
      android.R.id.text1, 
      R.id.userpic 
    }; 

    public CursorAdapter(Activity activity, Cursor c) { 
     super(activity, R.layout.contacts_item, c, FROM, TO, 0); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     // Here I logged cursor contents and am sure that thumbnail column data is null. 
     dumpCursorCurrent(cursor); 

     View view = super.newView(context, cursor, parent); 

     int columnIndex = cursor.getColumnIndexOrThrow(Contacts.PHOTO_THUMBNAIL_URI); 
     String thumbnailUriString = cursor.getString(columnIndex); 
     if (thumbnailUriString != null) { 
      throw new RuntimeException("Never comes here"); 
     } 
     return view; 
    } 
} 

코드 :

ContentResolver contentResolver = getActivity().getContentResolver(); 
    Cursor c = contentResolver.query(
      Contacts.CONTENT_URI, 
      projection, 
      null, 
      null, 
      null 
    ); 

그래서 목록에있는 각 연락처에 대한 적절한 사진이 사진.

나는 SimpleCursorAdapter 코드를 디버깅, 그리고 그것이 imageView.setImageURI(Uri.parse(""))를 호출하고 자연스럽게 각 행에 대해 경고 로그, 작동 안 :

1월 9일에서 3일까지 : 47 : 20.023 27814-27814/me.lazerka을 .mf.android E/BitmapFactory : 스트림 디코딩 할 수 없습니다 : java.io.FileNotFoundException를 :/: 개방 실패 : EISDIR이 (가 디렉토리) 09-03 01 : 47 : 20.023

27814-27814/나. lazerka.mf.android I/System.out : resolveUri이 (가) 에 잘못되었습니다. 비트 맵 오류 :

각 ImageView에 대해 적절한 imageURI를 설정하는 사람은 어디에서 가져 옵니까 (커서는 항상 null 임)?

+1

커서가 어디에서나 null이 있는지 확인 하시겠습니까? DatabaseUtils.dumpCursor()를 시도 했습니까? – pskink

+0

'DatabaseUtils' 제안에 감사드립니다. 사실 null이 없다는 것이 밝혀졌습니다. 트릭이'CursorAdapter # getView()'에 있습니다 - 사진이 null 인 항목이 있는데 null이 아닌 사진이있는 항목을 추가하고있었습니다. 그러나 getView()에서 newview()를 호출하지 않고 기존 뷰를 재사용했다. 여기서 중단 점을 넣었다. 그래서 이전에 null 사진과의 접촉을 위해 newView를 호출하고있었습니다. 포인트를 신경 쓰면 언제든지 답변을 게시하십시오. 받아 들일 것입니다. 고맙습니다. –

+1

지금 듣고 잘 듣고 싶습니다. – pskink

답변

0

SimpleCursorAdapter 구현시 숨겨진 문제 :보기가 다시 사용되었으며 마지막 목록 항목에 대해서만 newView()가 호출되었습니다. 따라서 연락처를 추가 할 때 이전 뷰를 다시 사용하고 적절한 사진을 설정하고 newView()를 호출하지 않았으므로 중단 점이 실행되지 않았습니다.