如何排序和过滤字符串数组?

时间:2015-07-13 22:22:11

标签: java android arrays sorting filter

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

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

<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

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

我有一个函数可以将string-array中的drawable的id添加到ArrayList,以便稍后只需编写drawables.get(position);来加载drawable:

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. 如何在将项目添加到brands之前对drawables进行排序。
  2. 如何创建过滤器功能以过滤brands并在RecylerView中正确更改内容。
  3. 我希望有人可以帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:2)

这是我的表现。

我创建了另一个ArrayList<Integer>和2 List<Array>,最后他们是&#34;变量:

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

然后当&#34;开始&#34;适配器,我这样排序字符串数组:

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

新的loadLogo void是:

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方法中,我写了这个:

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

我不知道这是否是正确的方法,但是我的目的是正常的。如果有人有更好的答案,我会很感激。

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

相关问题