动态自动完成textview显示缓慢,如何显示更快?

时间:2010-11-04 10:45:05

标签: android

我在自定义对话框中为autocompletetextview编写了一些代码。当键入一些文本时,文本会动态搜索到hashmap。这个hashmap是大行的文本。它很有用。但是慢慢给我结果。

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 = wordsSet.toArray(new String[0]);
        return wordsArray;
    }

如果我为上面的代码提取线程,它会给我线程处理程序异常。请给我一个关于autocomplettext上列表快速响应的想法。

Rajendar是

1 个答案:

答案 0 :(得分:0)

要确定哪些位快,哪些位慢,您需要使用Android's profiler。以下是值得研究的两件事,它们可能是最大的资源消耗:

  1. 每次按下某个键时,您正在编译正则表达式,这非常慢。更好的选择是填充数据库并进行查询。
  2. 每次按下一个键时,你都会创建一个TreeSet和一个数组,这可能会受到伤害。
  3. 将bookContentMap的值转换为String可能是处理器密集型的。考虑缓存此值。