2017-06-10 33 views
0

我想對我的應用程序執行搜索操作,並且我使用的AutoCompleteTextView使用多個存儲在資源上的字符串數組,我這樣做:使用多個資源字符串數組的AutoCompleteTextView

List<String> list = new ArrayList<>(); 

String[] array1 = getResources().getStringArray(R.array.array1); 
String[] array2 = getResources().getStringArray(R.array.array2); 
... 

list.addAll(Arrays.asList(array1)); 
list.addAll(Arrays.asList(array2)); 
... 

AutoCompleteTextView autoView = (AutoCompleteTextView) findViewById(R.id.auto_complete); 

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list); 

autoView.setAdapter(adapter); 

它很好用,但我的性能和內存我的應用程序,因爲每個數組上很多itens(總共超過400),所以我想知道是否創建一個列表來添加所有itens(就像我在做什麼)可能佔用太多內存? 這樣我做的是錯的還是對的?可能會損害應用程序性能?

任何人都知道嗎?

Thx

+0

你可以使你的自動完成文本視圖更高效。看看這個線程: https://stackoverflow.com/questions/30910098/efficient-autocompletetextview – sumit

回答

1

它不會產生太多問題。它只是消耗一點記憶。我使用Auto Complete TextView在列表中使用了大約8000個項目,這對我來說工作正常。 如果您擔心這件事,只需在兩種情況下檢查設備監視器上的內存使用情況。 您可以這樣做:

  • 通過ADB連接您的設備。
  • 運行您的應用程序。
  • 打開Logcat/Android Moniter。
  • 轉到監視器選項卡。
  • 在監視器選項卡中,您可以看到CPU,內存&網絡使用情況。

希望這會有所幫助。

+0

Thx爲您的幫助,順便說一句,你知道我必須做的,使屏幕儘快出現鍵盤與autoCompleteTextView出現?我試圖在autoCompleteTextView上添加'android:focusableI =「true」'和'android:focusableInTouchMode =「true」'但沒有工作:(而且當我點擊完成時鍵盤也不會消失......爲什麼? – Anita