动态更新的AutoCompleteTextView适配器 [英] Dynamically updating an AutoCompleteTextView adapter
问题描述
我要定期更改通过从RESTful Web服务添加到列表由AutoCompleteTextview给出的建议,并不能得到它的工作顺利进行。我设置了一个硬codeD的建议列表,以确保它的工作:
I want to periodically change the suggestions given by an AutoCompleteTextview by getting the list from a RESTful web service, and can't get it working smoothly. I set up a hard-coded list of suggestions to make sure it's working:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, new String[] {"Hi", "Ho"});
speciesName.setAdapter(adapter);//my autocomplete tv
我已经上了一个TextView的和TextWatcher当启动一个非阻塞调用文本更改为获取建议一个新的列表 - 这其中一部分得到一个新的列表工作正常。然后我想重置适配器,像这样:
I have got a TextWatcher on the textview and when the text changes that launches a non-blocking call to get a new list of suggestions -- this part which gets a new list is working fine. Then I want to reset the adapter, like so:
public void setOptionsAndUpdate(String[] options) {
Log.d(TAG, "setting options");
//speciesName.setAdapter((ArrayAdapter<String>)null);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, options);
speciesName.setAdapter(adapter);
}
此方法被调用,但是还是不行 - 建议列表中消失,要么不顾或调用 setAdapter
显示的建议保持不变。
This method is called, but doesn't work -- the list of suggestions either disappears or the displayed suggestions remain unchanged despite the call to setAdapter
.
这甚至正确的做法?我看了看 SimpleCursorAdapter
却不见如何注册我的Web服务作为一个内容提供商。 (这是形式 http://www.blah.com/query?term=XX ,其中XX的从我的应用程序输入,响应是字符串的JSON数组)。
Is this even the right approach? I looked at SimpleCursorAdapter
but couldn't see how to register my web service as a content provider. (It's of the form http://www.blah.com/query?term=XX, where the XX is the input from my app, and the response is a JSON Array of strings.)
推荐答案
这是我如何更新我AutoCompleteTextView:
This is how I update my AutoCompleteTextView:
String[] data = terms.toArray(new String[terms.size()]); // terms is a List<String>
ArrayAdapter<?> adapter = new ArrayAdapter<Object>(activity, android.R.layout.simple_dropdown_item_1line, data);
keywordField.setAdapter(adapter); // keywordField is a AutoCompleteTextView
if(terms.size() < 40) keywordField.setThreshold(1);
else keywordField.setThreshold(2);
现在当然,这是静态的,不以过度的空气处理的建议,但是,我也可以建议你通知适配器改变,你把它分配给了AutoCompleteTextView后:
Now of course, this is static and doesn't deal with an over-the-air suggestions but, I can also suggest you to notify adapter for the changes after you assign it to the AutoCompleteTextView:
adapter.notifyDataSetChanged();
希望这有助于。
-serkan
这篇关于动态更新的AutoCompleteTextView适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!