开发者

dynamic autocomplete textview displayes slowly,how to display faster?

I have written some code for autocompletetextview in custom dialog box.When typing some text that text dynamically search into the hashmap.This hashmap is with large lines of text.It works.But slowly giving me result.

AutoCompleteTextView searchText = (AutoCompleteTextView)searchDialog.findViewById(R.id.searchText);
    if(searchText.getText()!=null){
    //                  searchString = searchText.getText().toString().trim();
                    String[] autoList = getAutoCompletWords(searchText.getText().toString().trim());
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx,android.R.layout.simple_dropdown_item_1line,autoList);
                    searchText.setAdapter(adapter);
                    }

private String[] getAutoCompletWords(String text){
        Set<String> wordsSet = new TreeSet<String>();
        Pattern wordPattern = Pattern.compile("\\b"+text+"\\w+",Pattern.CASE_INSENSITIVE);
        Matcher matcher = wordPattern.matcher(bookContentMap.values().toString());
        while(matcher.find()){
            wordsSet.add(matcher.group());
        }
        String[] wordsArray = wordsS开发者_开发技巧et.toArray(new String[0]);
        return wordsArray;
    }

If I take thread for above code it is giving me thread handler exception.Please give me an idea for quick response of list on autocomplettext.

Rajendar Are


To know for sure which bits are fast and which are slow, you need to use Android's profiler. Here are two things worth investigating, they're probably the largest resource drain:

  1. You're compiling a regular expression each time a key is pressed, this is very slow. A better option would be to populate a database and query it instead.
  2. You're creating both a TreeSet and an Array each time a key is pressed, which probably hurts.
  3. Converting bookContentMap's values to a String is probably quite processor intensive. Consider caching this value.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜