gpt4 book ai didi

android - AlphabetIndexer 不适用于自定义 ListView

转载 作者:行者123 更新时间:2023-11-29 14:04:09 27 4
gpt4 key购买 nike

我正在为自定义 ListView 使用 AlphabetIndexer、SectionIndexer、自定义 CursorAdapter。以下是相同的代码。但是我在滚动列表时看不到字母表。我还尝试了以下链接 how to combine both DISPLAY_NAME and NUMBER in a customized CursorAdapter?

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();
}

}

我错过了什么吗?

最佳答案

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

关于android - AlphabetIndexer 不适用于自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8063549/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com