安卓:搜索从大的ArrayList [英] Android : Search from Large Arraylist

查看:93
本文介绍了安卓:搜索从大的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有29,000名记录的记录。我的屏幕中含有的EditText框包含所有29000记录搜索条件和ListView。

I have a recordset of about 29,000 records. My Screen contains EditText Box for Search Criteria and Listview containing all 29,000 records.

通过搜索它需要时间上市的方式,而不是给流更少的输出为我所需要的。

By searching with the listed way it takes time and not giving flow less output as I need.

我的EditText包含

My EditText contains

final EditText txtSearchCity = (EditText) findViewById(R.id.edtCity);
        txtSearchCity.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                aCountryIDTemp.clear();
                aCityStateTemp.clear();

                for (int i = 0; i < aCountryID.size(); i++) {
                    if (aCityState
                            .get(i)
                            .toLowerCase()
                            .contains(
                                    txtSearchCity.getText().toString()
                                            .toLowerCase())) {
                        aCountryIDTemp.add(aCountryID.get(i));
                        aCityStateTemp.add(aCityState.get(i));
                    }
                }

                BindList();
            }
        });
    }

BindList中()方法设置数组列表aCityStateTemp到适配器。 任何其他方式来搜索和动态创建新的ArrayList

BindList() method is setting the arraylist aCityStateTemp to adapter. Any Other way to Search and Create new ArrayList dynamically.

推荐答案

我会坚持使用的 Lambdaj 库是在您要限制循环进行排序和筛选集合此类案件大多采用。

I would insist to use Lambdaj Library which is mostly used in such cases where you want to restrict loops for sorting and filtering Collections.

下面是一个小例子,使用lambdaj过滤的ArrayList

Here is a small example for using lambdaj for filtering ArrayList.

ArrayList<String> sortedArrayList = select(arrList, having(on(String.class),
                                                   Matchers.containsString("a");

这将返回一个完整的滤波的ArrayList 与您要填充你的的ListView

This will return a complete filtered ArrayList with which you want to populate your ListView.

您也可以过滤器自定义类别 - Java的:什​​么是最好的方式来筛选集合?

You can also filter Custom Classes - Java: What is the best way to filter a Collection?

更新:

以上溶液区分大小写所以要解决,你可以添加的 多个匹配器

Above solution was case-sensitive so to work around you can add Multiple Matchers.

这样你可以添加多个匹配器

ArrayList<String> sortedArrayList = select(arrList, having(on(String.class),
   (Matchers.anyOf(Matchers.containsString("a"),Matchers.containsString("A")))));

更新:

更好的方法是使用过滤器(匹配器&LT;&GT;匹配,T ...阵列)

下面是你如何能做到这一点,

Here is how you can do that,

ArrayList<String> sortedArrayList = filter(Matchers.anyOf(
           Matchers.containsString("a"),Matchers.containsString("A")), arrList);

另外,如果你有兴趣使用一些 lambdaj 的方法/功能,可以提取源,并得到它的工作。我加入了相同的过滤器()

Also, if you are interested in using some of the methods/features of lambdaj, you can extract the source and get it working. I am adding the same for filter()

您可以下载 hamcrest-ALL-1.0.jar( 63 KB) 并添加以下code,以获得过滤器()工作

public static <T> List<T> filter(Matcher<?> matcher, Iterable<T> iterable) {
    if (iterable == null)
        return new LinkedList<T>();
    else{
        List<T> collected = new LinkedList<T>();
        Iterator<T> iterator = iterable.iterator();
        if (iterator == null)
            return collected;
        while (iterator.hasNext()) {
            T item = iterator.next();
            if (matcher.matches(item))
                collected.add(item);
        }
        return collected;
    }
}

所以,你可以整理出起码从 lambdaj 源并集成在源。

这篇关于安卓:搜索从大的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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