I'm trying to make an AutoCompleteTextView that shows suggestions from Google, but like the title says; the whole ArrayList is being shown as a suggestion instead of individual suggestions..
How do i show the separate strings, am I adding them to the adapter incorrectly?
Here is my custom 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]". The following parses the "everything_else" which is what interests you. 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());