0

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--> 
8
  • see CursorAdapter.setFilterQueryProvider() method Commented Oct 15, 2014 at 8:20
  • setFilterQueryProvider() in my understanding is used for filtering the results.How would it be used here? Commented Oct 15, 2014 at 10:32
  • just set SCA to your SearchView, you can pass null Cursor in SCA constructor, then call SCA.setFilterQueryProvider and in your FilterQueryProvider.runQuery return your MatrixCursor (note you dont need any background threads or async request as runQuery is called in the worker thread) Commented Oct 15, 2014 at 10:46
  • thanks for the explanation, i added the SCA's setFilterQueryProvider and returned the MatrixCursor in the runquery method, but it doesn't seem to work. Commented Oct 15, 2014 at 11:30
  • is runQuery being called? is the returned Cursor non empty? what actually doesn't work? Commented Oct 15, 2014 at 11:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.