2010-11-04 33 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