Ruby Man
Ruby Man

Reputation: 43

Android Search fast performance in listview

I am making a search application in android, my searching purpose is that when i type word in edittext then when the textchange, it will jump to select the position of the word that begin with that letter in listview. This is my code. it work well but the speed of text when i type or delete in edittext seems slow, not smoothly. What can i do to make it faster? I have over 20,000 entries from database.

txtword.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
    // TODO Auto-generated method stub      
    for(int i =0;i<list.size();i++) 
    { 
        if(list.get(i).toLowerCase().startsWith(s.toString())) 
        {
            pos = i;
            break; 
        } 
    } 
    lv.setSelection(pos);                   
}

Upvotes: 0

Views: 1669

Answers (3)

Timothy T.
Timothy T.

Reputation: 602

You may consider that iterating an ArrayList with over 20000 may take a lot of resource. I think you have to reevaluate the problem and how you can solve it.

Why don't you try to research via SQL request ? You can use the "LIKE" operator.

SQLite Android Doc Like operator example

Upvotes: 0

Abhishek Shukla
Abhishek Shukla

Reputation: 1242

You can use the patricia trie data structure to perform the search: Here are the typical steps:

  1. Populate your Strings into the patricia trie.
  2. Perform look-up for strings starting with the entered characterd in onTextChanged().It will return you a sub-trie. For next character entered, search in that sub-trie.
  3. When a text is removed from the editText, go back one level in the trie.

Here is the reference: https://code.google.com/p/patricia-trie/ And, here is a sample example: https://code.google.com/p/patricia-trie/wiki/Examples

Upvotes: 2

Sanket Kachhela
Sanket Kachhela

Reputation: 10856

you can user search filter in baseAdapter and arrayadapter for Listview. use this

1)https://stackoverflow.com/a/2726348/942224

2)https://stackoverflow.com/a/14359161/942224

to get detail information.

Upvotes: 0

Related Questions