I have used the Google places API to retrieve results through the implementation of setOnQueryTextListener of the searchView widget, then I am creating a SimpleCursorAdapter by using the results from the places API and setting it as suggestion adapter for the search view. The problem is that the adapter gets assigned and the List is displayed but the data is missing from the suggestion list.
Here is the code:
Activity Class
public static SearchView shopSearchView = null;
shopSearchView = (SearchView)menu.findItem(R.id.search).getActionView();
shopSearchView.setOnQueryTextListener(new OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
PlacesAutoComplete pl = new PlacesAutoComplete();
pl.autocomplete(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return true;
}
});
public class PlacesAutoComplete {
public static ServiceProvider serviceProvider = null; //Retrofit Service Endpoint
public static JsonArray placesPrediction = new JsonArray();
public void autocomplete(String input) {
final String[] columnNames = {"_id","description"};
final MatrixCursor suggestionCursor = new MatrixCursor(columnNames);
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("&input=" + input);
sb.append("?key=" + API_KEY);
sb.append("&components=country:in");
String url = sb.toString();
//using retrofit to get the data
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(url)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
serviceProvider = restAdapter.create(ServiceProvider.class);
serviceProvider.getPlacesSuggestion(new Callback<JsonObject>(){
@Override
public void failure(RetrofitError response) {
// TODO Auto-generated method stub
}
@Override
public void success(JsonObject result, Response response) {
placesPrediction = result.getAsJsonArray("predictions");
for (int i = 0; i < placesPrediction.size(); i++) {
Gson gson = new Gson();
PlacePrediction place= gson.fromJson(placesPrediction.get(i), PlacePrediction.class);
suggestionCursor.addRow(new Object[]{i,place.getDescription()});
//suggestionCursor is populated with the data. I can see the data while debugging.
}
int[] to = {R.id.name_entry};
SimpleCursorAdapter suggestionAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.list_item,suggestionCursor , new String[]{columnNames[1]},to, 0);
Activity.shopSearchView.setSuggestionsAdapter(suggestionAdapter);
//adapter is set, it is visible but the text is not shown
}
});
}
}
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/> <!--list_item.xml-->