具有 JSON 的自动完成文本视图
AutoCompleteTextView with JSON
我正在尝试将 AutoCompleteTextView 与 Json 一起使用,我正在使用本教程:this
我尝试向列表添加数据时遇到问题:foreach 不适用于类型 'com.example.program.model.Result'。下面是 AutoCompleteTextView 的模型数据和代码,我为示例模型添加了代码。
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());
...
}
型号
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;
}
}
您期望对象列表,但在 示例 class 中观察到单个对象。如果您的 API 抛出结果列表,则将示例 class 更改为
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;
}
}
并更改此行
for(Result result: ((Example) response.body()).getResult()){
strings.add(result.getTitle());
}
和
for(Result result: example.getResult()){
strings.add(result.getTitle());
}
我正在尝试将 AutoCompleteTextView 与 Json 一起使用,我正在使用本教程:this
我尝试向列表添加数据时遇到问题:foreach 不适用于类型 'com.example.program.model.Result'。下面是 AutoCompleteTextView 的模型数据和代码,我为示例模型添加了代码。
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());
...
}
型号
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;
}
}
您期望对象列表,但在 示例 class 中观察到单个对象。如果您的 API 抛出结果列表,则将示例 class 更改为
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;
}
}
并更改此行
for(Result result: ((Example) response.body()).getResult()){
strings.add(result.getTitle());
}
和
for(Result result: example.getResult()){
strings.add(result.getTitle());
}