解析Retrofit并将数据设置为基本适配器

时间:2017-11-17 12:30:14

标签: android

我正在使用RetrofitJSON Parsing Android)并且我是新手,我很困惑,无法使用基本适配器设置响应。

这是我的示例类

public class Example {

@SerializedName("Totalrecord")
@Expose
private List<Totalrecord> totalrecord = null;
@SerializedName("Table1")
@Expose
private List<Table1> table1 = null;
@SerializedName("Table2")
@Expose
private List<Table2> table2 = null;

public List<Totalrecord> getTotalrecord() {
    return totalrecord;
}

public void setTotalrecord(List<Totalrecord> totalrecord) {
    this.totalrecord = totalrecord;
}

public List<Table1> getTable1() {
    return table1;
}

public void setTable1(List<Table1> table1) {
    this.table1 = table1;
}

public List<Table2> getTable2() {
    return table2;
}

public void setTable2(List<Table2> table2) {
    this.table2 = table2;
}
}

此处我尝试在ArrayList中检索响应,以便将其设置为Adapter

    ApiService api = RetroClient.getApiService();
            Call<Example> call = api.getMyJson(38, 109);
            call.enqueue(new Callback<Example>() {
                @Override
                public void onResponse(Call<Example> call, Response<Example> response) {
                    dialog.dismiss();
                    if (response.isSuccessful()) {
                   //     Response_list = response.body().get...
                        adapter_view = new Adapter_view(Response_list, MainActivity.this);
                        listView.setAdapter(adapter_view);
                    }
                }
                @Override
                public void onFailure(Call<Example> call, Throwable t) {
                    dialog.dismiss();
                    System.out.println("failed");
                }
            });

但我很难设置ArrayList的类型以及Callback<>的类型,这样我才能正确获得响应并将ArrayList设置为Adapter。< / p>

2 个答案:

答案 0 :(得分:0)

如果您想在List<Totalrecord>中设置adapter,请尝试关注。

ApiService api = RetroClient.getApiService();
            Call<Example> call = api.getMyJson(38, 109);
            call.enqueue(new Callback<Example>() {
                @Override
                public void onResponse(Call<Example> call, Response<Example> response) {
                    dialog.dismiss();
                    if (response.isSuccessful()) {
                   Response_list.clear();
                   Response_list.addAll(call.getTotalrecord());
                        adapter_view = new Adapter_view(Response_list, MainActivity.this);
                        listView.setAdapter(adapter_view);
                    }
                }
                @Override
                public void onFailure(Call<Example> call, Throwable t) {
                    dialog.dismiss();
                    System.out.println("failed");
                }
            });

答案 1 :(得分:0)

List<Table1> mList=new ArrayList();
ApiService api = RetroClient.getApiService();
        Call<Example> call = api.getMyJson(38, 109);
        call.enqueue(new Callback<Example>() {
        @Override
  public void onResponse(Call<Example> call, Response<Example> response) {
                dialog.dismiss();
                if (response.body.isSuccessful()) {
                   mList=response.body.getTable1();
                   adapter_view = new Adapter_view(mList,MainActivity.this);
                    listView.setAdapter(adapter_view);
                }
            }
            @Override
            public void onFailure(Call<Example> call, Throwable t) {
                dialog.dismiss();
                System.out.println("failed");
            }
        });