Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SectionIndexer在模拟联系人应用程序中移动_Android_Scroll_Android Arrayadapter_Contacts_Sectionindexer - Fatal编程技术网

Android SectionIndexer在模拟联系人应用程序中移动

Android SectionIndexer在模拟联系人应用程序中移动,android,scroll,android-arrayadapter,contacts,sectionindexer,Android,Scroll,Android Arrayadapter,Contacts,Sectionindexer,编辑:文章底部的解决方案 我正在为android应用程序创建一个联系人屏幕,在这里我希望能够滚动浏览我的手机联系人。我可以导入手机联系人,使用自定义arrayadapter来组织我的信息,并为listview启用fastscroll选项。此外,当我滚动时,我能够将当前部分的第一个字母显示在我的拇指旁边。(即,如果我在名称以A开头的区域,则在滚动的拇指旁边的气泡中应显示“A”) 我的问题是,它不是根据当前位置显示正确的字母,而是在我所在的位置后面显示1个字母。例如,我在联系人的B部分,它显示字母“

编辑:文章底部的解决方案

我正在为android应用程序创建一个联系人屏幕,在这里我希望能够滚动浏览我的手机联系人。我可以导入手机联系人,使用自定义arrayadapter来组织我的信息,并为listview启用fastscroll选项。此外,当我滚动时,我能够将当前部分的第一个字母显示在我的拇指旁边。(即,如果我在名称以A开头的区域,则在滚动的拇指旁边的气泡中应显示“A”)

我的问题是,它不是根据当前位置显示正确的字母,而是在我所在的位置后面显示1个字母。例如,我在联系人的B部分,它显示字母“A”。有没有想过我做错了什么

这是我的适配器:

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()
    方法未返回正确的值
  • 实际上,您的索引器工作正常,但对您来说,它似乎工作异常。如果屏幕顶部(而不是列表顶部)的项目与您的节索引匹配,则它工作正常,因为它始终是决定您在列表中实际位置的顶部项目。因此,如果索引器显示字母“B”,并且屏幕上可见的第一个联系人以“B”开头,则是正确的(尽管当前可见的大多数联系人可能以“C”开头,这可能会导致错误的假设位于“C”部分)
  • 如果您可以确保这两件事都是正确的,请提供错误的屏幕截图和
    打印
    您的
    alphaIndexer
    映射


    尽管与您的问题没有直接关系,但您应该重新设计
    getSectionForPosition()
    (原文如此!)的实现,因为在浏览列表时返回常量可能会导致滚动条的位置不正确(有关更多信息,请参阅)