Android:从大型数组列表中搜索 [英] Android : Search from Large Arraylist

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

问题描述

我有大约 29,000 条记录.我的屏幕包含用于搜索条件的 EditText 框和包含所有 29,000 条记录的 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.

您还可以filter自定义类 - 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.

这样你可以添加Multiple Matchers,

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

更新:

更好的方法是使用 filter(Matcher matcher, T...array)

你可以这样做,

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

此外,如果您有兴趣使用 lambdaj 的某些方法/功能,您可以提取源代码并使其工作.我正在为 filter()

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)) 并添加以下代码以使 filter() 工作

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 源中挑选出最少的部分并集成到您的源中.

So, you can just sort out the least from lambdaj source and integrate in your source.

这篇关于Android:从大型数组列表中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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