2013-07-26 34 views
0

我做了一個自定義的CursorAdapter,用於在一行中顯示聯繫人照片,姓名和ID。自定義SimpleCursorAdapter重複職位

我覆蓋了getView()方法,等等,一切正常。我的問題是,現在,適配器用聯繫人1-8填充我的列表並重復它。 - > 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8。這是僅獲得1到8位置的結果,但我不明白爲什麼,因爲這是CursorAdapter在父類中的一部分,應該工作。

將我的CustomAdapter更改爲正常的SimpleCursorAdapter會以正確的形式顯示所有聯繫人。

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

    LayoutInflater vi; 

    View v; 

    CheckBox box; 
    v = convertView; 

    if (v == null) { 
     vi = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.gallery_item_layout, null); 
     ImageView image = (ImageView) v.findViewById(R.id.contactImage); 
     TextView tv = (TextView) v.findViewById(R.id.contactName); 
     Cursor cursor = (Cursor) getItem(position); 

     String id = cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Contacts._ID)); 
     tv.setText(position+" "+id+" " 
       + cursor.getString(cursor 
         .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); 
     String photoID = cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Contacts.PHOTO_ID)); 

     if (photoID != null) { 
      Uri photoUri = ContentUris.withAppendedId(
        ContactsContract.Data.CONTENT_URI, 
        Long.parseLong(photoID)); 
      image.setImageURI(photoUri); 

     } 
     box = (CheckBox) v.findViewById(R.id.checkBox); 
     box.setTag(id); 
     box.setOnCheckedChangeListener(this); 

    } 
    return v; 
} 

我的代碼有什麼問題?

回答

0

您正在重複使用相同的視圖,但不在其上輸入正確的信息。你可以看一下這個適配器視圖模式的教程。您最終將得到一個高效且快速的適配器,並提供正確的數據。

http://www.jmanzano.es/blog/?p=166

希望幫助:d

+0

我覺得viewholder模式無關與我的問題,該位置只算達到1〜8,即使我有接觸光標hundrets指向 –

+0

,但如果它不爲null,則您正在重複使用相同的視圖。我敢打賭,當你的應用程序啓動時,只能看到8個聯繫人。當滾動時再次調用適配器時,convertview是一個頂部消失的舊行,並且只檢查它是否爲空,然後將其返回。相信我,並試圖實現查看持有人模式:) –

+0

我從來沒有使用過它,但從來沒有遇到任何問題,但我會實現它:D –

相關問題