I am trying to use AutoCompleteTextView with Json and i am using this tutorial: this
I've got the problem when i try to add data to List: foreach not applicable to type 'com.example.program.model.Result'. Below is model data and code for AutoCompleteTextView and i added code for Example model .
private void DownloadGames() {
final AlertDialog alertDialog = new SpotsDialog.Builder()
.setContext(MainActivity.this)
.setTheme(R.style.CustomDialog)
.build();
alertDialog.setMessage("Loading Data... Please wait...");
alertDialog.setCancelable(true);
alertDialog.show();
Retrofit retrofit = GamesClient.getRetrofitClient();
GamesInterface gamesInterface = retrofit.create(GamesInterface.class);
Call call = gamesInterface.getGamesbyName(gameTitle.getText().toString(), SPINNER_POSITION);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
alertDialog.dismiss();
if (response.body() != null) {
Example example = (Example) response.body();
List<String> strings= new ArrayList<String>();
for(Result result: ((Example) response.body()).getResult()){
strings.add(result.getTitle());
}
ArrayAdapter<String> adapteo = new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_dropdown_item_1line, strings.toArray(new String[0]));
storeTV.setAdapter(adapteo);
contentTitle.setText(example.getResult().getReleaseDate());
...
}
Model
public class Result {
@SerializedName("title")
@Expose
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
public class Example {
@SerializedName("result")
@Expose
private Result result;
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
}
You expect List of objects but observe single object in Example class. If your API throws List of Results then change Example class with
public class Example {
@SerializedName("result")
@Expose
private List<Result> result;
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
}
And change this line
for(Result result: ((Example) response.body()).getResult()){
strings.add(result.getTitle());
}
with
for(Result result: example.getResult()){
strings.add(result.getTitle());
}