有没有在这个code任何错误,它显示了字母preVIEW快速滚动列表 [英] Is there any mistake in this code, which shows fast scroll list with alphabet preview

查看:126
本文介绍了有没有在这个code任何错误,它显示了字母preVIEW快速滚动列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题,在显示Android的ListView中第一个字母的preVIEW,当我做快速滚动我得到的文本preVIEW但将指向列表中错误的位置。

有关例如,请看看下面的图像,在此我们现在是在并购节,还是L字形正在出现。

下面是Listadapter code,一个实现了上述工艺,在code任何错误呢?

 类MyListAdaptor扩展ArrayAdapter<串GT;器物
        SectionIndexer
{    HashMap的<字符串,整数> alphaIndexer;
    的String []段;    公共MyListAdaptor(上下文的背景下,LinkedList的<串GT;项目){
        超(背景下,R.layout.list_item,项目);        alphaIndexer =新的HashMap<字符串,整数>();
        INT大小= items.size();        为(中间体X = 0; X&下;大小; X ++){
            字符串s = items.get(X);            //拿到店里的第一个字母
            串CH = s.substring(0,1);
            //转换为大写小写,否则一-z进行排序
            //后上A-Z
            CH = ch.toUpperCase();            // HashMap的将prevent重复
            alphaIndexer.put(CH,X);
        }        SET<串GT; sectionLetters = alphaIndexer.keySet();        //从集合创建列表进行排序
        ArrayList的<串GT; sectionList =新的ArrayList<串GT;(
                sectionLetters);        Collections.sort(sectionList);        部分=新的String [sectionList.size()];        sectionList.toArray(路段);
    }    公众诠释getPositionForSection(INT部分){
        返回alphaIndexer.get(第[节]);
    }    公众诠释getSectionForPosition(INT位置){
        返回0;
    }    公共对象[] getSections(){
        返回段;
    }
}


解决方案

通过使用 alphaIndexer.put(CH,X); 来prevent重复,你是保持最后位置的元素开始 CH ,而不是第一个。这是因为每次调用把比第一其他与给定键更新旧值。与此code尝试,你会更近了一步:

 如果(!alphaIndexer.containsKey(CH))
    alphaIndexer.put(CH,X);

I am facing problem in showing the preview of first letter in Android listview, when i do fast scroll i get the preview of text but it will be pointing to the wrong location in list.

For eg, please have a look at the below image, in this now we are in M section, still L letter is appearing.

Here is the Listadapter code, which implements the above Technic, any mistake in the code?

    class MyListAdaptor extends ArrayAdapter<String> implements
        SectionIndexer 
{

    HashMap<String, Integer> alphaIndexer;
    String[] sections;

    public MyListAdaptor(Context context, LinkedList<String> items) {
        super(context, R.layout.list_item, items);

        alphaIndexer = new HashMap<String, Integer>();
        int size = items.size();

        for (int x = 0; x < size; x++) {
            String s = items.get(x);

            // get the first letter of the store
            String ch = s.substring(0, 1);
            // convert to uppercase otherwise lowercase a -z will be sorted
            // after upper A-Z
            ch = ch.toUpperCase();

            // HashMap will prevent duplicates
            alphaIndexer.put(ch, x);
        }

        Set<String> sectionLetters = alphaIndexer.keySet();

        // create a list from the set to sort
        ArrayList<String> sectionList = new ArrayList<String>(
                sectionLetters);

        Collections.sort(sectionList);

        sections = new String[sectionList.size()];

        sectionList.toArray(sections);
    }

    public int getPositionForSection(int section) {
        return alphaIndexer.get(sections[section]);
    }

    public int getSectionForPosition(int position) {
        return 0;
    }

    public Object[] getSections() {
        return sections;
    }
}

解决方案

By using alphaIndexer.put(ch, x); to prevent duplicates, you are keeping the last position for an element starting with ch instead of the first one. That is because each call to put other than the first with a given key will update the old value. Try with this code and you will be one step closer:

if( !alphaIndexer.containsKey(ch) )
    alphaIndexer.put(ch, x);

这篇关于有没有在这个code任何错误,它显示了字母preVIEW快速滚动列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆