2011-05-19 56 views
2

我想为我的用户在文本框中输入文本时具有自动完成功能。在android中使用AutoCompleteTextView可以轻松实现。最初,我建立/res/values/strings.xml我的字符串数组作为android:AutoCompleteTextView的字符串数组大小限制

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string-array name="autocomplete_list"> 
    <item>text 1</item> 
    <item>text 2</item> 
    <item>... (many more) ...</item> 
    <item>text 1499</item> 
    <item>text 1500</item> 
</string-array> 
</resources> 

在我的Java代码,我有

// load autocomplete list 
    String[] textList= getResources().getStringArray(R.array.autocomplete_list); 
    myAutoCompleteTextView.setAdapter(new ArrayAdapter<String>(this, R.layout.autocomplete_list, textList)); 

这工作得很好,当我只有几件事情我串。但是,如果我的列表很长,说超过1000项,我得到运行时错误。应用程序将自行关闭,没有任何警告(没有应用程序强制关闭错误)。它只是消失。 logcat中没有任何内容。

所以我超过Java中的字符串数组中的最大元素可索引?那是什么导致了这个问题?如果是这样,任何人都知道最大值是多少?它看起来不像内存问题,因为我没有得到OutOfMemory错误。

编辑:

因此很明显,这是众所周知的问题,似乎是对的getResource()getStringArray的限制()限制

Should I be using something other than getResource().getStringArray() to populate a large array?

将首先尝试他的解决方案,看看如何。以及它的作品,但如果你知道一个更好的方式来做到这一点,请随时分享。谢谢!

回答

1

我也发现这个问题。最后,我必须将我的数组拆分为多个元素,使用getResources()。getStringArray()读取每个元素,然后使用普通Java将它们连接在一起,形成一个ArrayList或类似的类。

相关问题