如何排序和过滤字符串数组? [英] How to sort and filter a string-array?

查看:81
本文介绍了如何排序和过滤字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对字符串数组中的项目列表进行排序,然后使用工具栏中的 SearchView 进行过滤。

I want to sort a list of items in a string-array and then filter them using a SearchView in the toolbar.

这是字符串数组(每个项目都是res文件夹中可绘制的png的名称):

This is the string-array (each item is the name of a png drawable in res folder):

<string-array name="brands">
    <item>facebook</item>
    <item>twitter</item>
    <item>instagram</item>
    <item>android</item>
    <item>blackberry</item>
    <item>samsung</item>
    <item>huawei</item>
    <item>starbucks</item>
    <item>motorola</item>
    <item>nexus</item>
    <item>lg</item>
    <item>beats</item>
    <item>sony</item>
    <item>lenovo</item>
    <item>dell</item>
    <item>hp</item>
</string-array>

RecyclerView 适配器中,我已设置数组和 ArrayList

In a RecyclerView Adapter, I have set the array and an ArrayList:

private String[] brands;
private ArrayList<Integer> drawables;

我有一个函数可以从字符串中添加可绘制对象的ID -array ArrayList ,这样我以后就可以通过简单地编写 drawables.get(position);来加载可绘制对象了。

And I have a function that adds the id of the drawables from the string-array to the ArrayList so I can later load the drawable by simply writing drawables.get(position); :

private void loadLogo() {
    drawables = new ArrayList<>();

    brands = context.getResources().getStringArray(R.array.brands);

    for (String extra : brands) {
        int res = context.getResources().getIdentifier(extra, "drawable", context.getPackageName());
        if (res != 0) {
            final int brandInt = context.getResources().getIdentifier(extra, "drawable", context.getPackageName());
            if (brandInt != 0)
                drawables.add(brandInt);
        }
    }
}

我想知道的是:


  1. 在将项目添加到之前如何对品牌进行排序drawables

  2. 如何创建过滤功能来过滤品牌并在 RecylerView 正确。

  1. How to sort brands before adding items to drawables.
  2. How to create a filter function to filter brands and make the content change in the RecylerView properly.

我希望有人可以帮助我。

I hope someone can help me. Thanks in advance.

推荐答案

这是我的操作方式。

我创建了另一个 ArrayList< Integer> 和2个 List< Array> ,最后它们是变量:

I created another ArrayList<Integer> and 2 List<Array>, at the end they were the "variables:

private ArrayList<Integer> drawables, mFiltered;
private String[] brands;
private List<String> stringList, mFilteredNames;
int resId;

然后在启动适配器时,我

Then when "starting" the adapter, I sorted the string array this way:

stringList = new ArrayList<String>(Arrays.asList(brands));
Collections.sort(stringList);
loadLogo(stringList);

新的loadLogo无效是:

The new loadLogo void is:

private void loadLogo(List<String> list) {
        drawables = new ArrayList<>();

        for (String extra : list) {
            int res = r.getIdentifier(extra, "drawable", p);
            if (res != 0) {
                final int brandInt = r.getIdentifier(extra, "drawable", p);
                if (brandInt != 0)
                    drawables.add(brandInt);
            }
        }
    }

这是我的过滤功能:

public synchronized void filter(CharSequence s) {
        if (s == null || s.toString().trim().isEmpty()) {
            if (mFiltered != null) {
                mFiltered = null;
                notifyDataSetChanged();
            }
        } else {
            if (mFiltered != null)
                mFiltered.clear();
            mFiltered = new ArrayList<>();
            mFilteredNames = new ArrayList<String>();
            for (int i = 0; i < stringList.size(); i++) {
                final String name = stringList.get(i);
                if (name.toLowerCase(Locale.getDefault())
                        .startsWith(s.toString().toLowerCase(Locale.getDefault()))) {
                    mFiltered.add(drawables.get(i));
                    mFilteredNames.add(name);
                }
            }
            notifyDataSetChanged();
        }

并在 onBindViewHolder RecyclerView 适配器的方法,我这样写:

And in the onBindViewHolder method of the RecyclerView adapter, I wrote this:

if (mFiltered != null) {
            resId = mFiltered.get(position);
            holder.logo.setImageResource(resId);
        } else {
            resId = drawables.get(position);
            holder.logo.setImageResource(resId);
        }

我不知道这是否是正确的方法为我的目的而努力。如果有人有更好的答案,我将不胜感激。

I don't know if it's the right way to do it, but is working for my purpose. If someone has a better answer I will be thankful.

此外,我不知道这对其他人有多大用处,因为它主要用于自定义目的,但我希望这对其他人也有帮助。

Also, I don't know how useful this could be for other people as it is mostly for a custom purpose, but I hope this helps someone else too.

这篇关于如何排序和过滤字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆