2

セクション インデクサーを使用して、並べ替えられたアルファベット順のアプリでカスタム ListView を作成したいと考えています。ArrayList<String>セクション インデクサーは、セクション インデックス機能を持つカスタム アダプターをどのように作成すればよいかを扱うだけなので、このように同じ:

ここに画像の説明を入力

いくつかの解決策を提案してください。

それは私のコード クラス MyAZAdapter extends ArrayAdapter implements SectionIndexer { ArrayList myElements; HashMap azIndexer; 文字列[] セクション; アプリを一覧表示します。

    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
    }
}

マイ アプリのアクティビティ コード

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);      
4

1 に答える 1

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

直前に上記のコードが必要です。

myListView.setAdapter(adapter);
于 2016-01-19T19:56:41.433 に答える