簡體   English   中英

如何在Listview中設置改造響應正文數據

[英]How to set Retrofit response body data in Listview

Daata函數中,嘗試從服務器獲取數據。 已成功獲取,但無法在ArrayList中設置數據

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

因為我想設置在FlowerAdapter flowerListModels數據並顯示在列表視圖

  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;
    }

}

您正在將空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();
    }
});
}

您將在此處創建新的空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);
    }

像這樣創建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);

干杯!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM