开发者

Android List view SectionIndexer - list view not displaying curent scroll letter

开发者 https://www.devze.com 2023-04-06 16:35 出处:网络
I am trying to use secionIndexer with fast scroll in list view. I haveimplemented but on scroll of list it should popup the current letter at which we have scrolled,

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号