Android SectionIndexer在模拟联系人应用程序中移动
编辑:文章底部的解决方案 我正在为android应用程序创建一个联系人屏幕,在这里我希望能够滚动浏览我的手机联系人。我可以导入手机联系人,使用自定义arrayadapter来组织我的信息,并为listview启用fastscroll选项。此外,当我滚动时,我能够将当前部分的第一个字母显示在我的拇指旁边。(即,如果我在名称以A开头的区域,则在滚动的拇指旁边的气泡中应显示“A”) 我的问题是,它不是根据当前位置显示正确的字母,而是在我所在的位置后面显示1个字母。例如,我在联系人的B部分,它显示字母“A”。有没有想过我做错了什么 这是我的适配器:Android SectionIndexer在模拟联系人应用程序中移动,android,scroll,android-arrayadapter,contacts,sectionindexer,Android,Scroll,Android Arrayadapter,Contacts,Sectionindexer,编辑:文章底部的解决方案 我正在为android应用程序创建一个联系人屏幕,在这里我希望能够滚动浏览我的手机联系人。我可以导入手机联系人,使用自定义arrayadapter来组织我的信息,并为listview启用fastscroll选项。此外,当我滚动时,我能够将当前部分的第一个字母显示在我的拇指旁边。(即,如果我在名称以A开头的区域,则在滚动的拇指旁边的气泡中应显示“A”) 我的问题是,它不是根据当前位置显示正确的字母,而是在我所在的位置后面显示1个字母。例如,我在联系人的B部分,它显示字母“
public class ContactAdapter extends ArrayAdapter<Contact> implements SectionIndexer {
Context context;
int layoutResourceId;
ArrayList<Contact> contacts = null;
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
public ContactAdapter(Context context, int layoutResourceId, ArrayList<Contact> contacts) {
super(context, layoutResourceId, contacts);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.contacts = contacts;
Collections.sort(this.contacts);
alphaIndexer = new HashMap<String, Integer>();
for (int x = 0; x < this.contacts.size(); x++)
alphaIndexer.put(this.contacts.get(x).getSection(), x);
Set<String> sectionLetters = alphaIndexer.keySet();
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
sectionList.toArray(sections);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Additional code here
}
static class ContactHolder {
TextView txtName;
CheckBox checked;
}
public int getPositionForSection(int section) {
return alphaIndexer.get(sections[section]);
}
public int getSectionForPosition(int position) {
return 0;
}
public Object[] getSections() {
return sections;
}
}
公共类ContactAdapter扩展ArrayAdapter实现SectionIndexer{
语境;
国际布局资源;
ArrayList contacts=null;
私有HashMap字母索引器;
私有字符串[]节;
公共联系人适配器(上下文上下文、int-layoutResourceId、ArrayList联系人){
超级(上下文、布局资源ID、联系人);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个。联系人=联系人;
Collections.sort(this.contacts);
alphaIndexer=新HashMap();
对于(int x=0;x
以下是最终实现此功能的代码:
public ContactAdapter(Context context, int layoutResourceId, ArrayList<Contact> contacts) {
super(context, layoutResourceId, contacts);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.contacts = contacts;
this.results = new ArrayList<Contact>();
Collections.sort(this.contacts);
// Keys are the different sections, values are the indexes at which those sections start
alphaIndexer = new HashMap<String, Integer>();
// Only set the value for each key (section) when we encounter a new section
String prevSection = "";
for (int x = 0; x < this.contacts.size(); x++) {
String section = this.contacts.get(x).getSection();
if (!prevSection.equals(section)) {
alphaIndexer.put(section, x);
prevSection = section;
}
}
Set<String> sectionLetters = alphaIndexer.keySet();
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
sectionList.toArray(sections);
}
public ContactAdapter(上下文上下文、int-layoutResourceId、ArrayList联系人){
超级(上下文、布局资源ID、联系人);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个。联系人=联系人;
this.results=新的ArrayList();
Collections.sort(this.contacts);
//键是不同的部分,值是这些部分开始的索引
alphaIndexer=新HashMap();
//仅在遇到新节时为每个键(节)设置值
字符串prevSection=“”;
对于(int x=0;x
在我看来,您的实现看起来还不错。我认为有两种可能性:
Contact.getSection()
方法未返回正确的值打印您的alphaIndexer
映射
尽管与您的问题没有直接关系,但您应该重新设计getSectionForPosition()
(原文如此!)的实现,因为在浏览列表时返回常量可能会导致滚动条的位置不正确(有关更多信息,请参阅)