【问题标题】:Custom Alphabetical sorted listview with sections带有部分的自定义按字母顺序排序的列表视图
【发布时间】:2016-01-19 19:04:52
【问题描述】:

我想使用 Indexer 部分创建一个带有按字母顺序排序的应用程序的自定义 ListView。由于 Section Indexer 仅处理 ArrayList<String> 所以我如何编写具有部分索引功能的自定义适配器。像这样:

请提出一些解决方案。

这是我的代码 MyAZAdapter 类扩展 ArrayAdapter 实现 SectionIndexer { 数组列表我的元素; 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
    }
}

我的 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);      

【问题讨论】:

  • 您需要发布一些您尝试过的代码。这不是免费的编码服务。我们只能通过查看您的代码来提供帮助。
  • 我已经提供了我试过的代码。

标签: android listview android-custom-view sectionindexer


【解决方案1】:
adapter.sort(new Comparator<String>() {
    public int compare(String object1, String object2) {
        return object2.compareTo(object1);
    };
});

你之前需要上面的代码:

myListView.setAdapter(adapter);

【讨论】:

  • @Graiel 我已经在我的 getInstalledApplication 方法中这样做了,例如 'Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(packageManager));'但我想知道的是如何使用自定义布局制作我的适配器项目。如果你在上面的图片中看到一个图标和一个应用程序名称,所以我需要这样的列表视图项目布局。但不要改变什么来做这样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
  • 1970-01-01
  • 2018-09-15
  • 2012-11-02
  • 2021-04-02
  • 2017-12-16
相关资源
最近更新 更多