2

I want to make a Custom ListView with sorted alphabetical apps using section Indexer. Since Section Indexer only treat with ArrayList<String> so how do I write custom adapter that have section indexing functionality. Same like this:

enter image description here

Please suggest some solution.

Thats My code class MyAZAdapter extends ArrayAdapter implements SectionIndexer { ArrayList myElements; HashMap azIndexer; String[] sections; List 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
    }
}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
    }
}

My AppsActivity code

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);      
Ali Akram
  • 199
  • 4
  • 12

1 Answers1

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

you need the above code right before:

myListView.setAdapter(adapter);
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • @Grariel I am doing it already in my getInstalledApplication Method like this 'Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(packageManager));' But what i want to know is how to make my adapter item using custom layout. If you look in above image there is an icon and a app name so i need listview item layout like that.But dont what to change to make like that. – Ali Akram Jan 19 '16 at 20:04