How to dynamically add suggestions to autocompletetextview with preserving character status(如何在保留字符状态的情况下动态向 autocompletetextview 添加建议)
问题描述
问题描述:
我在使用 AutoCompleteTextView 时遇到了一些问题,我必须在每次按键后显示建议.问题是,建议列表是动态的,就像谷歌的建议功能一样.这意味着应在用户不断输入时添加新建议,并应显示所有匹配的旧建议.
I am facing some problem with AutoCompleteTextView where I have to show suggestions after each keypress. Thing is that, list of suggestion is dynamic like google's suggestion feature. It means the new suggestions should be added as user keeps typing in plus all matching old suggestions should be displayed.
例如
我写te",然后它应该显示以前的建议,如test1"和test2"以及我将从 Web API 获得的新建议.假设 web api 给了我单词tea"&紧张".
I write "te" and then it should display previous suggestions like "test1" & "test2" and the new suggestions that I will get from Web API. Suppose web api gives me word "tea"& "tension ".
现在 AutoCompleteTextView 将有te"作为字符串,下面显示所有四个建议.
Now the AutoCompleteTextView will have "te" as string with all four suggestions showing below it.
这正是我想要的.
看起来很简单,但它表现出一种奇怪的行为.
looks simple but it is showing a strange behaviour.
我正在使用全局声明的默认 ArrayAdapter 类实例.
I am using default ArrayAdapter class instance of which I am declaring globally.
arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,suggestions);
word.setAdapter(arrayAdapter);
建议是 ArrayList.
suggestions is ArrayList.
从 WebApi 获得新结果后,我只需调用
Upon getting new result from WebApi I simply call
arrayAdapter.notifyDataSetChanged();
刷新数据观察者和附加的视图(在我们的例子中是 AutoCompleteListView).
to refresh the data observer and views attached with this (in our case AutoCompleteListView).
但它会关闭建议.
当我不使用 notifyDataSetChanged();
时,无论我输入什么字符,它都会显示所有建议.
When I don't use notifyDataSetChanged();
it is showing all suggestions regardless of characters I have typed.
我尝试了许多建议的自定义过滤器,但它们都没有帮助,因为我无法使用 notifyDataSetChanged().
I tried it with custom filter as many suggested but none of them is helpful as I couldn't use notifyDataSetChanged().
我发布图片以避免混淆.
I am posting an image to avoid confusions.
我很困惑为什么 notifyDataSetChanged();
它不起作用.我没有使用具有相同 arrayAdapter 实例的任何其他列表引用.我真的怀疑这是否是一个参考问题.
I have a confusion that why notifyDataSetChanged();
its not working. I haven't use any other reference of list with same arrayAdapter instance. I really doubt if it's a reference problem.
推荐答案
最简单的方法之一(将代码放在 onCreate 中):
one of the easiest way of doing that (put the code in onCreate):
添加 wikipedia free opensearch(如果 https://en.wikipedia.org 不起作用,请尝试http://en.wikipedia.org)
addied wikipedia free opensearch (if https://en.wikipedia.org doesn't work try http://en.wikipedia.org)
AutoCompleteTextView actv = new AutoCompleteTextView(this);
actv.setThreshold(1);
String[] from = { "name", "description" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter a = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, null, from, to, 0);
a.setStringConversionColumn(1);
FilterQueryProvider provider = new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
// run in the background thread
Log.d(TAG, "runQuery constraint: " + constraint);
if (constraint == null) {
return null;
}
String[] columnNames = { BaseColumns._ID, "name", "description" };
MatrixCursor c = new MatrixCursor(columnNames);
try {
String urlString = "https://en.wikipedia.org/w/api.php?" +
"action=opensearch&search=" + constraint +
"&limit=8&namespace=0&format=json";
URL url = new URL(urlString);
InputStream stream = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String jsonStr = reader.readLine();
// output ["query", ["n0", "n1", ..], ["d0", "d1", ..]]
JSONArray json = new JSONArray(jsonStr);
JSONArray names = json.getJSONArray(1);
JSONArray descriptions = json.getJSONArray(2);
for (int i = 0; i < names.length(); i++) {
c.newRow().add(i).add(names.getString(i)).add(descriptions.getString(i));
}
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
};
a.setFilterQueryProvider(provider);
actv.setAdapter(a);
setContentView(actv, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
这篇关于如何在保留字符状态的情况下动态向 autocompletetextview 添加建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在保留字符状态的情况下动态向 autocompletetextview 添加建议


- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01