Filter Json Results For Autocomplete Adapter
I have an autocomplete adapter for my app and I am getting back json results from the Google Places API. My trouble is with trying to get the results to display on the screen. I am
Solution 1:
I see you have an AsyncTask
to access your data from the server, but you don't really need an AsyncTask
because performFiltering()
already runs in the background, so you can put your doInBackground()
logic in performFiltering()
.
@Overrideprotected FilterResults performFiltering(CharSequence constraint) {
ArrayList<String> resultList = null;
FilterResultsfilterResults=newFilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
Log.d(TAG, "Starting Search");
HttpURLConnectionconn=null;
StringBuilderjsonResults=newStringBuilder();
try {
KEYWORD = constraint.toString();
ENDPOINT = Uri
.parse("https://maps.googleapis.com/maps/api/place/nearbysearch/json")
.buildUpon()
.appendQueryParameter("location", LOCATION)
.appendQueryParameter("radius", "10000")
.appendQueryParameter("keyword", KEYWORD)
.appendQueryParameter("key", API_KEY)
.build();
URLurl=newURL(ENDPOINT.toString());
Log.d(TAG, "URL: " + url);
conn = (HttpURLConnection) url.openConnection();
InputStreamReaderin=newInputStreamReader(conn.getInputStream());
// Load the results into a StringBuilderint read;
char[] buff = newchar[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
Log.d(TAG, jsonResults.toString());
} catch (MalformedURLException e) {
Log.e(TAG, "Error processing Places API URL", e);
return filterResults ;
} catch (IOException e) {
Log.e(TAG, "Error connecting to Places API", e);
return filterResults ;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the resultsJSONObjectjsonObj=newJSONObject(jsonResults.toString());
JSONArraypredsJsonArray= jsonObj.getJSONArray("results");
// Extract the Place descriptions from the results
resultList = newArrayList(predsJsonArray.length());
for (inti=0; i < predsJsonArray.length(); i++) {
System.out.println(predsJsonArray.getJSONObject(i).getString("name"));
System.out.println(predsJsonArray.getJSONObject(i).getString("vicinity"));
System.out.println("=====================================================");
resultList.add(predsJsonArray.getJSONObject(i).getString("name"));
resultList.add(predsJsonArray.getJSONObject(i).getString("vicinity"));
}
} catch (JSONException e) {
Log.e(TAG, "Cannot process JSON results", e);
}
Log.d(TAG, resultList.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
A Filter
works like an AsyncTask
. performFiltering
is like doInBackground
and publishResults()
is like onPostExecute()
.
Incidentally, don't update the adapter list in a background thread. Create a new list and assign it to FilterResults.values
, then assign the adapter list in publishResults()
.
publishResults()
should look like this:
@OverrideprotectedvoidpublishResults(CharSequence constraint, Filter.FilterResults results) {
if (results != null && results.count > 0) {
resultList = (ArrayList) results.values;
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
Post a Comment for "Filter Json Results For Autocomplete Adapter"