Android 列表视图SectionIndexer - 列表视图不显示当前滚动字母

发布于 2024-12-05 12:28:07 字数 4087 浏览 4 评论 0原文

我正在尝试在列表视图中使用 secionIndexer 进行快速滚动。 我已经实施了但是 在列表滚动上,它应该弹出我们滚动的当前字母, 但它没有显示这一点。

在调试模式下,我发现在我的情况下永远不会调用 getSectipons() 和 getPositionForSection() , 但在我从网络上尝试的任何其他简单示例中,它确实会调用这些函数。

请建议我该怎么做 //################## 这是我的适配器的代码

//#######################

public class MyListAdapter extends ArrayAdapter<Object> implements SectionIndexer {

    Activity context;
    Object[] listData;
    HashMap<String, Integer> alphaIndexer;
    String[] sections;

    public MyListAdapter(Activity context, Object[] objects) {
        super(context, R.layout.new_invest_row, objects);
        this.context = context;
        listData = objects;
        alphaIndexer = new HashMap<String, Integer>();
        //changes here

         ArrayList myArrayList = new ArrayList();
           for (int ix=0; ix<objects.length; ix++) {
               myArrayList.add(objects[ix]);
               }

           Collections.sort(myArrayList, new Comparator<HashMap<String, String>>(){ 
                public int compare(HashMap<String, String> one, HashMap<String, String> two) { 
                    return one.get("Name").compareToIgnoreCase(two.get("Name"));
                } 
        });

         listData = myArrayList.toArray();
        //Arrays.sort(listData);
        for (int i = listData.length - 1; i >= 0; i--) {
            final HashMap<String, String> obj = (HashMap<String, String>)listData[i];
            String element = obj.get("Name");
            alphaIndexer.put(element.substring(0, 1).toUpperCase(), i); 
        }

        Set<String> keys = alphaIndexer.keySet(); // set of letters

        // Copy into a List for sorting
        Iterator<String> it = keys.iterator();
        ArrayList<String> keyList = new ArrayList<String>(); 
        while (it.hasNext()) {
            String key = it.next();
            keyList.add(key);
        }
        Collections.sort(keyList);

        // Convert to array
        sections = new String[keyList.size()];
        keyList.toArray(sections);
        for(int c=0;c < sections.length;c++)Log.e("secction<"+c+">","+"+sections[c]);
        Log.e("alphaIndexer","+"+alphaIndexer);
    }

    static class ViewHolder {
        TextView txtName;
        TextView txtOwner;
        TextView txtStartDate;
        TextView txtEndDate;
        TextView txtStatus;
        ImageView imgIcon;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.new_invest_row, null, true);
            holder = new ViewHolder();
            **holder.txtName = (TextView) rowView.findViewById(R.id.invest_row_name);**
//my custom code here

            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        @SuppressWarnings("unchecked")
        final HashMap<String, String> obj = (HashMap<String, String>)listData[position];

        String str = obj.get("Name");
        if(null == str){
            str = "";
        }
        holder.txtName.setText(str);

        //#################
               //my custom code here

        return rowView;
    }

    public int getPositionForSection(int section) {
        String letter = sections[section];
        Log.e("alphaIndexer.get(letter)","+"+alphaIndexer.get(letter));

        return alphaIndexer.get(letter);
    }

    public int getSectionForPosition(int arg0) {
        // TODO Auto-generated method stub
        return 1;
    }

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

//#######################

我假设在适配器中,当我传递 textViewId 时,它不会将当前的 textVewId 作为永远不会调用sectionIndexer 函数。

在布局 new_invest_row 中,我得到了带有图标和几个文本视图的自定义行。 我根据每行中显示的对象名称对列表进行排序。 我希望索引器能够处理对象的名称。

请帮我提供准确的解决方案

I am trying to use secionIndexer with fast scroll in list view.
I have implemented but
on scroll of list it should popup the current letter at which we have scrolled,
but its not showing that.

on debug mode I came to know that getSectipons() and getPositionForSection() are never called in my case ,
but in any other simple example that i tried from web it does make a call to those functions.

Pelase suggest me what to do
//##################
here is code of my adapter

//#######################

public class MyListAdapter extends ArrayAdapter<Object> implements SectionIndexer {

    Activity context;
    Object[] listData;
    HashMap<String, Integer> alphaIndexer;
    String[] sections;

    public MyListAdapter(Activity context, Object[] objects) {
        super(context, R.layout.new_invest_row, objects);
        this.context = context;
        listData = objects;
        alphaIndexer = new HashMap<String, Integer>();
        //changes here

         ArrayList myArrayList = new ArrayList();
           for (int ix=0; ix<objects.length; ix++) {
               myArrayList.add(objects[ix]);
               }

           Collections.sort(myArrayList, new Comparator<HashMap<String, String>>(){ 
                public int compare(HashMap<String, String> one, HashMap<String, String> two) { 
                    return one.get("Name").compareToIgnoreCase(two.get("Name"));
                } 
        });

         listData = myArrayList.toArray();
        //Arrays.sort(listData);
        for (int i = listData.length - 1; i >= 0; i--) {
            final HashMap<String, String> obj = (HashMap<String, String>)listData[i];
            String element = obj.get("Name");
            alphaIndexer.put(element.substring(0, 1).toUpperCase(), i); 
        }

        Set<String> keys = alphaIndexer.keySet(); // set of letters

        // Copy into a List for sorting
        Iterator<String> it = keys.iterator();
        ArrayList<String> keyList = new ArrayList<String>(); 
        while (it.hasNext()) {
            String key = it.next();
            keyList.add(key);
        }
        Collections.sort(keyList);

        // Convert to array
        sections = new String[keyList.size()];
        keyList.toArray(sections);
        for(int c=0;c < sections.length;c++)Log.e("secction<"+c+">","+"+sections[c]);
        Log.e("alphaIndexer","+"+alphaIndexer);
    }

    static class ViewHolder {
        TextView txtName;
        TextView txtOwner;
        TextView txtStartDate;
        TextView txtEndDate;
        TextView txtStatus;
        ImageView imgIcon;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.new_invest_row, null, true);
            holder = new ViewHolder();
            **holder.txtName = (TextView) rowView.findViewById(R.id.invest_row_name);**
//my custom code here

            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        @SuppressWarnings("unchecked")
        final HashMap<String, String> obj = (HashMap<String, String>)listData[position];

        String str = obj.get("Name");
        if(null == str){
            str = "";
        }
        holder.txtName.setText(str);

        //#################
               //my custom code here

        return rowView;
    }

    public int getPositionForSection(int section) {
        String letter = sections[section];
        Log.e("alphaIndexer.get(letter)","+"+alphaIndexer.get(letter));

        return alphaIndexer.get(letter);
    }

    public int getSectionForPosition(int arg0) {
        // TODO Auto-generated method stub
        return 1;
    }

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

//#######################

I assume that in adapter when I am passing the textViewId its not taking the currect textVewId as the sectionIndexer functions are never called.

In the Layout new_invest_row I am getting custom row which have an icon and few textViews.
I am sorting the list on the basis of name of the Object that I am displaying in each row.
i want indexer to work on the name of the object.

Please help me with exact solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文