【发布时间】:2020-08-12 14:09:19
【问题描述】:
我想从“画廊”数组中加载所有图像,但只加载了一张图像,n 也 /length() 不适用于数组长度。
我在这里提供了 json,如果你可以在 viewpager 或任何滑块中加载所有“g_image”,请给我代码
private void LoadGalleryImages(ApiService apiService, String ProductId) {
Gallery gallery = new Gallery();
gallery.setId(ProductId);
Call<ApiResponse<DataProduct>> galleryDetails = apiService.getGalleryDetails(gallery);
galleryDetails.enqueue(new Callback<ApiResponse<DataProduct>>() {
@Override
public void onResponse(@NonNull Call<ApiResponse<DataProduct>> call, @NonNull Response<ApiResponse<DataProduct>> response) {
SliderUtils sliderUtils = new SliderUtils();
try {
ApiResponse<DataProduct> resp = response.body();
DataProduct gallerys = resp.getData();
if (gallerys != null) {
Gallery gallery = gallerys.getGallery().get(0);
galleryCache = gallery;
for (int i = 0; i < 4; i++) {
Toast.makeText(getActivity(), "Image"+ i, Toast.LENGTH_SHORT).show();
sliderUtils.setSliderImageUrl(gallery.getGalleryImage());
}
sliderImg.add(sliderUtils);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), "ERROR RESPONSE" , Toast.LENGTH_SHORT).show();
}
viewPagerBannerAdapter = new ViewPagerBannerAdapter(sliderImg, getActivity());
viewPager.setAdapter(viewPagerBannerAdapter);
}
@Override
public void onFailure(Call<ApiResponse<DataProduct>> call, Throwable t) {
}
});
}
这是 JSON 数据
{
"status": "success",
"data": {
"product": [
{
"id": "8917",
"name": "test 5",
"description": "Not Available",
"price": "9999",
"image": "image_link",
"discounts_description": "",
"discount": "0.00",
"discount_type": "",
"unit": "Piece",
"url": "test-8917",
"category_id": "356",
"balance_qty": "0"
}
],
"relative_products": [],
"gallery": [
{
"g_image": "image_link"
},
{
"g_image": "image_link"
},
{
"g_image": "image_link"
}
]
}
}
而且我也无法获取数组长度..(.length()) 不起作用
数据产品类
public class DataProduct {
@SerializedName("product")
@Expose
private List<Product> product = null;
@SerializedName("relative_products")
@Expose
private List<Product> relativeProducts = null;
@SerializedName("gallery")
@Expose
private List<Gallery> gallery = null;
public List<Gallery> getGallery() {
return gallery;
}
public void setGallery(List<Gallery> gallery) {
this.gallery = gallery;
}
public List<Product> getProduct() {
return product;
}
public void setProduct(List<Product> product) {
this.product = product;
}
public List<Product> getRelativeProducts() {
return relativeProducts;
}
public void setRelativeProducts(List<Product> relativeProducts) {
this.relativeProducts = relativeProducts;
}
@Override
public String toString() {
return "DataProduct{" +
"product=" + product +
", relativeProducts=" + relativeProducts +
", gallery=" + gallery +
'}';
}
}
【问题讨论】:
-
能否请您发布您的
DataProduct课程?