如何在Listview中设置Retrofit响应正文数据

时间:2017-07-25 11:15:56

标签: java android retrofit2

Daata 函数中尝试从服务器获取数据。已成功获取,但数据无法在ArrayList中设置

List<FlowerListModel>flowerListModels=new ArrayList<>();

原因我想在 FlowerAdapter 中设置 flowerListModels 数据并在listview中显示

  public void Daata() {
    Call<List<FlowerListData>>listCall=apiInterface.getflowers();
    listCall.enqueue(new Callback<List<FlowerListData>>() {
        @Override
        public void onResponse(Call<List<FlowerListData>> call, Response<List<FlowerListData>> response) {
            Log.d("DataCheck",new Gson().toJson(response.body()));
            List<FlowerListModel>flowerListModels=new ArrayList<>();

          FlowerAdapter flowerAdapter = new FlowerAdapter(getApplicationContext(),flowerListModels);
            listView.setAdapter(flowerAdapter);
        }
        @Override
        public void onFailure(Call<List<FlowerListData>> call, Throwable t) {
            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
        }
    });
}

这是 FlowerListModel

package bdservers.com.schoolmanagement.Model;

public class FlowerListModel {

    private String category;
    private String instructions;
    private String photo;
    private String name;
    private String price;

    public FlowerListModel(){}
    public FlowerListModel(String category, String instructions, String photo, String name,String price){
        this.category=category;
        this.instructions=instructions;
        this.photo=photo;
        this.name=name;
       this.price=price;
       }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getInstructions() {
        return instructions;
    }

    public void setInstructions(String instructions) {
        this.instructions = instructions;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }

}

3 个答案:

答案 0 :(得分:2)

您正在为适配器设置空ArrayList,我已突出显示您发生错误的行,以及您需要的正确行

public void Daata() {
Call<List<FlowerListData>>listCall=apiInterface.getflowers();
listCall.enqueue(new Callback<List<FlowerListData>>() {
    @Override
    public void onResponse(Call<List<FlowerListData>> call, Response<List<FlowerListData>> response) {
        Log.d("DataCheck",new Gson().toJson(response.body()));

        /**
        * You are setting this empty list to adapter
        *List<FlowerListModel>flowerListModels=new ArrayList<>();
        */

        List<FlowerListModel> flowerListModels = new ArrayList<>();
        flowerListModels = response.body();

      FlowerAdapter flowerAdapter = new FlowerAdapter(getApplicationContext(),flowerListModels);
        listView.setAdapter(flowerAdapter);
    }
    @Override
    public void onFailure(Call<List<FlowerListData>> call, Throwable t) {
        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
    }
});
}

答案 1 :(得分:0)

您在此处创建了新的空ListList<FlowerListModel>flowerListModels=new ArrayList<>();

您可以尝试这样的事情:

    @Override
    public void onResponse(Call<List<FlowerListData>> call, Response<List<FlowerListData>> response) {
        Log.d("DataCheck",new Gson().toJson(response.body()));
        FlowerAdapter flowerAdapter = new FlowerAdapter(getApplicationContext(),response.body());
        listView.setAdapter(flowerAdapter);
    }

答案 2 :(得分:0)

像这样创建BaseResponse模型

public class BaseResponse {

    @SerializedName("data")
    private List<Object> alObjects;

    public BaseResponse(List<Object> alObjects) {
        this.alObjects = alObjects;
    }

    public List<Object> getAlObjects() {
        return alObjects;
    }

    public void setAlObjects(List<Object> alObjects) {
        this.alObjects = alObjects;
    }

}

然后从服务器获取数据

@POST(Constants.URL_API_DATA)
BaseResponse executeBaseResponse(@Body String mData);

干杯!!