我已经使用Google Places API通过searchView小部件的setOnQueryTextListener的实现来检索结果,然后我通过使用places 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-->