2010-11-04 73 views
0

我已经写了autocompletetextview一些代码自定义对话框box.When键入一些文字,文本搜索动态进入hashmap.This HashMap的是大线text.It works.But慢慢给我造成的。动态自动完成textview缓慢显示,如何显示更快?

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

如果我采取上面的代码是给我线程处理器exception.Please给我上autocomplettext列表的快速响应的想法线程。

Rajendar是

回答

0

要确信这些位是快和慢,你需要使用Android's profiler。下面是值得研究两件事情,他们很可能最大的资源流失:

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

其实iam使用treeset来排序项目,同时避免重复 – ADIT 2010-11-08 12:55:58

+0

@ user486216噢好吧。创建字符串也可能相当密集。我会编辑并添加 – 2010-11-08 13:00:45