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

问题描述:

Daata函数尝试从服务器获取数据。成功取出,但数据不能在ArrayList中如何在Listview中设置Retrofit响应正文数据

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

原因设置我想设置flowerListModels数据FlowerAdapter,并显示在列表视图

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

} 
+0

试试这个正确的路线https://www.androidhive.info/2016/05/android-working-with-retrofit -http-library/ –

+0

你的意思是'不能在ArrayList中设置' –

+0

你似乎没有对你得到的响应数据做任何事情。您需要使用作为响应获取的数据中的条目填充新创建的数组列表(flowerListModels)。否则,你只是试图显示一个空的数组。 – hildegard

您正在创建新的空List这里:List<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); 
    } 
+0

在FlowerAdapter不能应用于(上下文,java.util.List )在FlowerAdapter中显示此错误 –

从服务器

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

干杯创建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; 
    } 

} 

然后得到的数据!

你们是空虚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(); 
    } 
}); 
} 
+0

无法应用 至 (上下文, java.util.List ) 显示此错误 –