0

我尝试了这段代码,但是遍历所有列表项太耗时了。 item_all是一个列表,其中包含我从数据库中获得的数据。

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);
    }

然后我尝试使用 LIKE

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();
}

它工作得很好,但它并没有真正做到我想要的。

我想要的是搜索的项目与输入完全匹配。Aay 我输入“abc”,我不希望出现仅包含“ab”或“bc”(不是“abc”)的项目。

4

1 回答 1

0

尝试使用contains()而不是equalsIgnoreCase().

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

这可能会帮助你。:)

于 2012-08-14T05:36:19.100 回答