Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带列表的Android AutoCompleteTextView<;字符串>;_Android_List_Adapter_Autocompletetextview - Fatal编程技术网

带列表的Android AutoCompleteTextView<;字符串>;

带列表的Android AutoCompleteTextView<;字符串>;,android,list,adapter,autocompletetextview,Android,List,Adapter,Autocompletetextview,我有一个AutoCompleteTextView,用户在其中键入地址。我想在他打字时能在下面给出建议。为此,我通过反向地理编码API获得了一个可能的地址列表。然后我想向用户显示这个字符串列表(可能的地址)。就像谷歌地图应用程序一样 我已将TextChangedListener添加到AutoCompleteTextView中。在onTextChanged()事件上执行AsyncTask,其中可能的地址列表在onPostExecute()上得到更新 尝试1 名单如下: static List<

我有一个
AutoCompleteTextView
,用户在其中键入地址。我想在他打字时能在下面给出建议。为此,我通过反向地理编码API获得了一个可能的地址列表。然后我想向用户显示这个字符串列表(可能的地址)。就像谷歌地图应用程序一样

我已将
TextChangedListener
添加到
AutoCompleteTextView
中。在
onTextChanged()
事件上执行
AsyncTask
,其中可能的地址列表在
onPostExecute()
上得到更新

尝试1

名单如下:

static List<String> suggestionList = new ArrayList<String>();
上面的代码没有显示任何内容

尝试2

我还尝试使用数组作为适配器的参数,每次更新地址列表时,我都将其转换为数组

static String[] suggestions;
static List<String> suggestionList = new ArrayList<String>();

我怎样才能让它工作?有没有办法将适配器与列表一起使用?

没有显示任何内容的原因是列表为空,并且在AsyncTask的
onPostExecute
中,只有将新数组分配给
建议
参考,您真正应该做的是使用适配器方法添加和删除元素。您可以尝试以下代码:

adapter.clear();
adapter.addAll(/*new collection of suggestions*/);
在您的
onPostExecute
方法中


注意:
adapter.addAll()
方法仅在第11个API中出现,因此如果使用lower,则必须手动添加每个项

没有显示任何内容的原因是列表为空,并且在AsyncTask的
onPostExecute
中,只有将新数组分配给
建议
引用,您真正应该做的是使用适配器方法添加和删除元素。您可以尝试以下代码:

adapter.clear();
adapter.addAll(/*new collection of suggestions*/);
在您的
onPostExecute
方法中

注意:
adapter.addAll()
方法仅在第11个API中出现,因此如果使用lower,则必须手动添加每个项

autoText.setThreshold(3);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, suggestionList);
autoText.setAdapter(adapter);
adapter.setNotifyOnChange(true);
protected void onPostExecute(Void aVoid) {
...
suggestions = suggestionList.toArray(new String[suggestionList.size()]);
super.onPostExecute(aVoid);
}
adapter.clear();
adapter.addAll(/*new collection of suggestions*/);