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);
}
then I tried to use 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();
}
It works great except but it does not really do what I want.
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…