自定义按字母排序的列表视图与部分

时间:2016-01-19 19:04:53

标签: android listview android-custom-view sectionindexer

我想使用部分索引器制作带有排序字母应用的自定义列表视图。由于Section Indexer仅处理ArrayList<String>,因此如何编写具有节索引功能的自定义适配器。同样如下:

enter image description here

请提出一些解决方案。

多数民众赞成我的代码     class MyAZAdapter extends ArrayAdapter实现了SectionIndexer {         ArrayList myElements;         HashMap azIndexer;         String []部分;         列出应用程序;

    public MyAZAdapter(Context context, int textViewResourceId, List<T> objects, List<ApplicationInfo> apps) {
        super(context, textViewResourceId, objects);
        myElements = (ArrayList<String>) objects;
        azIndexer = new HashMap<String, Integer>(); //stores the positions for the start of each letter
        this.apps = apps;
        int size = elements.size();
        for (int i = size - 1; i >= 0; i--) {
            String element = elements.get(i);
            //We store the first letter of the word, and its index.
            azIndexer.put(element.substring(0, 1), i);
        }

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

        Iterator<String> it = keys.iterator();
        ArrayList<String> keyList = new ArrayList<String>();

        while (it.hasNext()) {
            String key = it.next();
            keyList.add(key);
        }
        Collections.sort(keyList);//sort the keylist
        sections = new String[keyList.size()]; // simple conversion to array
        keyList.toArray(sections);
    }

    public int getPositionForSection(int section) {
        if (section == 35) {
            return 0;
        }
        for (int i = 0; i < myElements.size(); i++) {
            String l = myElements.get(i);
            char firstChar = l.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

    public int getSectionForPosition(int position) {
        Log.v("getSectionForPosition", "called");
        return 0;
    }

    public Object[] getSections() {
        return sections; // to string will be called to display the letter
    }
}class MyAZAdapter<T> extends ArrayAdapter<T> implements SectionIndexer {
    ArrayList<String> myElements;
    HashMap<String, Integer> azIndexer;
    String[] sections;
    List<ApplicationInfo> apps;

    public MyAZAdapter(Context context, int textViewResourceId, List<T> objects, List<ApplicationInfo> apps) {
        super(context, textViewResourceId, objects);
        myElements = (ArrayList<String>) objects;
        azIndexer = new HashMap<String, Integer>(); //stores the positions for the start of each letter
        this.apps = apps;
        int size = elements.size();
        for (int i = size - 1; i >= 0; i--) {
            String element = elements.get(i);
            //We store the first letter of the word, and its index.
            azIndexer.put(element.substring(0, 1), i);
        }

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

        Iterator<String> it = keys.iterator();
        ArrayList<String> keyList = new ArrayList<String>();

        while (it.hasNext()) {
            String key = it.next();
            keyList.add(key);
        }
        Collections.sort(keyList);//sort the keylist
        sections = new String[keyList.size()]; // simple conversion to array
        keyList.toArray(sections);
    }

    public int getPositionForSection(int section) {
        if (section == 35) {
            return 0;
        }
        for (int i = 0; i < myElements.size(); i++) {
            String l = myElements.get(i);
            char firstChar = l.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

    public int getSectionForPosition(int position) {
        Log.v("getSectionForPosition", "called");
        return 0;
    }

    public Object[] getSections() {
        return sections; // to string will be called to display the letter
    }
}

我的AppsActivity代码

elements = new ArrayList<String>();
    List<ApplicationInfo> apps = getInstalledApplication(getActivity());
    for (int i = 0; i < apps.size(); i++) {
        elements.add(apps.get(i).loadLabel(getActivity().getPackageManager()).toString());
    }
    Collections.sort(elements,String.CASE_INSENSITIVE_ORDER); // Must be sorted!

    // listview
    myListView = (ListView) rootView.findViewById(R.id.myListView);
    //myListView.setFastScrollEnabled(true);
    MyAZAdapter<String> adapter = new MyAZAdapter<String>(
            getActivity(), android.R.layout.simple_list_item_1,
            elements, apps);
    myListView.setAdapter(adapter);
    SideBar indexBar = (SideBar) rootView.findViewById(R.id.sideBar);
    indexBar.setListView(myListView);      

1 个答案:

答案 0 :(得分:0)

adapter.sort(new Comparator<String>() {
    public int compare(String object1, String object2) {
        return object2.compareTo(object1);
    };
});

您之前需要上述代码:

myListView.setAdapter(adapter);