通过在Android ListView项和数据库搜索 [英] Searching through ListView item and database on Android

查看:124
本文介绍了通过在Android ListView项和数据库搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这code,但它太时采取通过所有的列表项进行迭代。
item_all 是包含我的数据从数据库中得到了一个清单。

I tried this code but it's too time-taking to iterate through all the list item. item_all is a list which contains data I got from the database.

et_Search.addTextChangedListener(new TextWatcher() {

    int textlength;
    long time = System.currentTimeMillis();

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        textlength = getSearchText().length();

        item_filtered = new ArrayList<StockItem>();
        long time = System.currentTimeMillis();

        int i = 0;

        do {
            int startpost = 0;
            int endpost = textlength;
            boolean found;
            do {
                found = false;
                if (getItemDesc(i).length() >= textlength 
                    && getSearchText().equalsIgnoreCase((String)getItemDesc(i).subSequence(startpost, endpost))) {
                    item_filtered.add(item_all.get(i));
                    found = true;
                }

                startpost++;
                endpost++;
            } while (endpost <= getItemDesc(i).length() && !found);

            i++;
        } while ((i < item_all.size()) && (item_filtered.size() <= 20));

        Log.v("Time", "Time needed " + (System.currentTimeMillis() - time));

        if (item_filtered.isEmpty())
            AppendList(new ArrayList<StockItem>());
        else
            AppendList(item_filtered);
    }

然后我试图用像

try {
    String[] WHATYOUWANT = { "*" };
    String WHERECLAUSE = KEY_DESCRIPTION + " LIKE '%" + textIinput + "%' " ;
    String GROUPBY = null;
    String HAVING = null;
    String ORDERBY = KEY_ROWID + " ASC LIMIT 0,100 ";

    cursor = mDb.query(DATABASE_TABLE, WHATYOUWANT, WHERECLAUSE, null, GROUPBY, HAVING, ORDERBY);

    } catch (Exception e) {
        e.printStackTrace();
}

但它的伟大工程,但它并没有真正做我想做的。

It works great except but it does not really do what I want.

我要的是搜索到的项目正好与输入匹配。 AAY我输入ABC,我不希望只包含AB或BC项目(而不是ABC)的出现。

What I want is the searched item match exactly with the input. Aay I input "abc", I don't want items which contain only "ab" or "bc" (not "abc") to appear.

推荐答案

尝试使用包含()而不是 equalsIgnoreCase()

 if (getItemDesc(i).length() >= textlength
                            && getItemDesc(i)
                                    .contains(getSearchText()));

这可能会帮助你的。 :)

This might help you. :)

这篇关于通过在Android ListView项和数据库搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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