[英]How to set Retrofit response body data in Listview
In Daata function try to fetch data from server. 在Daata函数中,尝试从服务器获取数据。 Successfully fetched, but data cant be set in ArrayList 已成功获取,但无法在ArrayList中设置数据
List<FlowerListModel>flowerListModels=new ArrayList<>();
Cause i want to set flowerListModels data in FlowerAdapter and show in listview 因为我想设置在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();
}
});
}
Here is FlowerListModel class 这是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;
}
}
You are setting empty ArrayList
to your adapter, I have highlighted the line where you have made the error, and also the correct line that you need 您正在将空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();
}
});
}
You are creating new empty List
here: List<FlowerListModel>flowerListModels=new ArrayList<>();
您将在此处创建新的空List
: List<FlowerListModel>flowerListModels=new ArrayList<>();
You can try something like this: 您可以尝试如下操作:
@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);
}
Create BaseResponse model like this 像这样创建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;
}
}
Then get data from server 然后从服务器获取数据
@POST(Constants.URL_API_DATA)
BaseResponse executeBaseResponse(@Body String mData);
Cheers!! 干杯!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.