2011-11-09 89 views
2

我正在使用AlphabetIndexer,SectionIndexer,自定義CursorAdapter自定義ListView。以下是相同的代碼。但是在滾動列表時我無法看到字母。我也試過以下鏈接how to combine both DISPLAY_NAME and NUMBER in a customized CursorAdapter?AlphabetIndexer不能使用自定義ListView

public class ContactListCustomCursorAdapter extends CursorAdapter implements 
    SectionIndexer { 

private LayoutInflater inflater; 
private CursorsForContacts cursorsForContacts; 
private AlphabetIndexer alphabetIndexer; 

public ContactListCustomCursorAdapter(Context context, Cursor cursor) { 
    super(context, cursor, true); 
    inflater = LayoutInflater.from(context); 
    cursorsForContacts = new CursorsForContacts(context); 
    alphabetIndexer = new AlphabetIndexer(
      cursor, 
      cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME), 
      " ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 
    //alphabetIndexer.setCursor(cursor); 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 

    String contactId = setContactName(view, cursor); 
    setContactPhoneNumbers(view, contactId); 
    setContactEmails(view, contactId); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    final View view = inflater.inflate(R.layout.custom_contact_list_item, 
      parent, false); 

    return view; 
} 

private String setContactName(View view, Cursor cursor) { 
    int username = cursor 
      .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME); 
    TextView userName = (TextView) view.findViewById(R.id.userName); 
    userName.setText(cursor.getString(username)); 

    return cursor.getString(cursor 
      .getColumnIndex(ContactsContract.Contacts._ID)); 

} 

private void setContactPhoneNumbers(View view, String contactId) { 
    Cursor phoneCursor = cursorsForContacts.getPhoneNumberCursor(contactId); 
    TextView phoneNumber = (TextView) view.findViewById(R.id.userNumber); 
    while (phoneCursor.moveToNext()) { 
     phoneNumber 
       .setText(phoneCursor.getString(phoneCursor 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
    } 
} 

private void setContactEmails(View view, String contactId) { 
    Cursor emailCursor = cursorsForContacts.getEmailCursor(contactId); 
    TextView userEmailId = (TextView) view.findViewById(R.id.userEmailId); 
    while (emailCursor.moveToNext()) { 
     userEmailId 
       .setText(emailCursor.getString(emailCursor 
         .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))); 
    } 
} 

@Override 
public int getPositionForSection(int section) { 
    return alphabetIndexer.getPositionForSection(section); 
} 

@Override 
public int getSectionForPosition(int position) { 
    return alphabetIndexer.getSectionForPosition(position); 
} 

@Override 
public Object[] getSections() { 
    return alphabetIndexer.getSections(); 
} 

} 

我是否錯過了什麼?

+0

我正在做類似的東西這裏http://stackoverflow.com/questions/10224233/alphabetindexer-with-custom-adapter-managed-by-loadermanager – toobsco42

+0

@Nik同樣在這裏..你有什麼發現?導致我的搜索已經到頭了,沒有成功.. – JcDenton86

+0

你試過這個[教程](https://eshyu.wordpress.com/2010/08/15/cursoradapter-with-alphabet-indexed-section-headers/) ? – JcDenton86

回答

0

您需要將android:fastScrollEnabled="true"添加到佈局xml中的ListView中。

0

可能爲時已晚,OP但誰同樣的問題可能會受益這裏絆倒其他人......

在我的情況字母索引是「隨機」出現在某些情況下,並沒有出現在其他國家,同一個屏幕上。

原來如果列表不是很長,快速滾動,因此字母索引器將被暫停。確切的行爲可以在類FastScroller中找到,該類是AbsListView的幫助類。有一段代碼有沒有決定是否「列表很長」

final boolean longList = childCount > 0 && itemCount/childCount >= MIN_PAGES; 

MIN_PAGES與4.你有它,如果你的列表項數不至少4倍的兒童計(價值定義可見行)快速滾動,因此字母索引器不會出現。