Google プレイス API を使用して、searchView ウィジェットの setOnQueryTextListener の実装を通じて結果を取得しました。次に、プレイス API からの結果を使用して SimpleCursorAdapter を作成し、それを検索ビューの提案アダプターとして設定しています。問題は、アダプタが割り当てられ、リストが表示されますが、候補リストにデータが表示されないことです。
コードは次のとおりです。
活動クラス
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-->