[英]AutoCompleteTextView Custom ArrayAdapter showing whole ArrayList instead of separate strings
I'm trying to make an AutoCompleteTextView that shows suggestions from Google, but like the title says; 我正在尝试制作一个AutoCompleteTextView来显示来自Google的建议,但是就像标题中所说的那样; the whole ArrayList is being shown as a suggestion instead of individual suggestions.. 整个ArrayList被显示为一个建议,而不是单个建议。
How do i show the separate strings, am I adding them to the adapter incorrectly? 如何显示单独的字符串,是否将它们错误地添加到适配器中?
Here is my custom ArrayAdapter 这是我自定义的ArrayAdapter
class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> data;
private static final String BASE_URL = "http://suggestqueries.google.com/complete/search?";
private static final String CLIENT = "client=firefox"; // firefox = JSON | toolbar = toolbar
private static final String SEARCH_URL = BASE_URL + CLIENT + "&q=";
private final static String UTF8 = "UTF-8";
AutoCompleteAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
this.data = new ArrayList<>();
}
@Override
public int getCount() {
return data.size();
}
@Nullable
@Override
public String getItem(int index) {
return data.get(index);
}
@NonNull
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null) {
HttpURLConnection conn = null;
InputStream input = null;
try {
URL url = new URL(SEARCH_URL + constraint.toString());
conn = (HttpURLConnection) url.openConnection();
input = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(input, UTF8);
BufferedReader buffer = new BufferedReader(reader, 8192);
StringBuilder builder = new StringBuilder();
String line;
while ((line = buffer.readLine()) != null) {
builder.append(line);
}
JSONArray terms = new JSONArray(builder.toString());
ArrayList<String> suggestions = new ArrayList<>();
for (int i = 0; i < terms.length(); i++) {
String term = terms.getString(i);
suggestions.add(term);
}
results.values = suggestions;
results.count = suggestions.size();
data = suggestions;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (conn != null) conn.disconnect();
}
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else notifyDataSetInvalidated();
}
};
}
} }
I did some searching to see if other people have had trouble with the same thing but I couldn't find anything relevant. 我做了一些搜索,看看其他人是否在同一件事上遇到了麻烦,但是我找不到任何相关的东西。
I've been pulling my hair out over this for ages now.. Thank you in advance. 我已经在这上面拉了好多年了。.预先谢谢您。
It looks like the response that you are seeing is "[search_term, [result1, result2...]]", so when you are parsing you are getting an array of length two: "[search_term, everything_else]". 看起来您看到的响应是“ [search_term,[result1,result2 ...]]”,因此,当您进行解析时,将得到长度为2的数组:“ [search_term,everything_else]”。 The following parses the "everything_else" which is what interests you. 下面分析了您感兴趣的“ everything_else”。 I recommend that you just use this as a starting point since you may need to do some additional error checking: 我建议您仅以此为起点,因为您可能需要执行一些其他错误检查:
JSONArray terms = new JSONArray(builder.toString());
terms = new JSONArray(terms.get(1).toString());
please check this parsing...
JSONArray terms = new JSONArray(builder.toString());
ArrayList<String> suggestions = new ArrayList<>();
for (int i = 0; i < terms.length(); i++) {
String term = terms.getString(i);
suggestions.add(term);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.