[英]how to get list of lists from json?
我試圖在另一個recyclerview中創建一個recyclerview,並從包含列表列表的API獲取數據。 我成功地在主recyclerview中顯示了數據,但是在子recyclerview中,列表的大小為空。
我試圖制作一個主recyclerview,並在MainAdapter中將其稱為seconed適配器,但在第二個適配器上卻得到了一個空大小,我只需要有人告訴我為什么會出現此錯誤,盡管它已成功加載了數據
{
"code": 200,
"Status": "success",
"data": [
{
"id": 3,
"ar_title": "رجالي",
"en_title": "Men",
"img": "1561730519.bb2.jpg",
"subCategories": [
{
"id": 6,
"ar_title": "قمصان",
"en_title": "Shirts",
"img": "1566590131.celine-druguet-V2WjELpgOEQ-unsplash.jpg"
},
{
"id": 8,
"ar_title": "تيشريت بولو",
"en_title": "Polos",
"img": "1563560646.bg_1.jpg"
},
{
"id": 9,
"ar_title": "بناطيل",
"en_title": "Jeans & Pants",
"img": "1563560646.bg_1.jpg"
},
{
"id": 10,
"ar_title": "شورتات",
"en_title": "Shorts",
"img": null
},
{
"id": 11,
"ar_title": "تيشيرتات",
"en_title": "T-shirts",
"img": null
}
]
},
{
"id": 4,
"ar_title": "حريمي",
"en_title": "Women",
"img": "1561815572.background.jpg",
"subCategories": [
{
"id": 12,
"ar_title": "فساتين",
"en_title": "Dresses",
"img": null
},
{
"id": 13,
"ar_title": "تيشيرتات",
"en_title": "Tops",
"img": "1563560646.bg_1.jpg"
},
{
"id": 14,
"ar_title": "بناطيل",
"en_title": "Jeans & Pants",
"img": "1563560646.bg_1.jpg"
},
{
"id": 15,
"ar_title": "ليقنز",
"en_title": "Leggings",
"img": null
},
{
"id": 16,
"ar_title": "تنانير",
"en_title": "Skirts",
"img": null
},
{
"id": 17,
"ar_title": "ملابس نوم",
"en_title": "Sleepwear",
"img": null
}
]
}
]
}
AndroidNetworking.get(Constant.BASE_URL+"Categories")
.setPriority(Priority.MEDIUM)
.build()
.getAsObject(AllCategoriesResponse.class, new ParsedRequestListener<AllCategoriesResponse>() {
@Override
public void onResponse(AllCategoriesResponse response) {
/*progress.dismiss();*/
if(response.getCode().equals("200"))
{
if(response.getData().size() > 0)
initCategoriesList(response.getData());
}
else
{
if(response.getData().size() > 0){
initCategoriesList(response.getData());
}
Toast.makeText(getApplicationContext(),getString(R.string.api_failure_message),Toast.LENGTH_LONG).show();
}
}
@Override
public void onError(ANError anError) {
/*
progress.dismiss();
*/
Toast.makeText(getApplicationContext(),getString(R.string.api_failure_message),Toast.LENGTH_LONG).show();
}
});
public class AllCategoriesResponse {
@SerializedName("code")
private Long mCode;
@SerializedName("data")
private List<Datum> mData;
@SerializedName("Status")
private String mStatus;
//set and get
public class Datum {
@SerializedName("ar_title")
private String mArTitle;
@SerializedName("en_title")
private String mEnTitle;
@SerializedName("id")
private Long mId;
@SerializedName("img")
private String mImg;
@SerializedName("subCategories")
private List<SubCategory> mSubCategories;
//set and get
public class SubCategory {
@SerializedName("ar_title")
private String mArTitle;
@SerializedName("en_title")
private String mEnTitle;
@SerializedName("id")
private Long mId;
@SerializedName("img")
private String mImg;
//set and get
}
}
}
public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.ViewHolder> {
Context context;
private List<AllCategoriesResponse.Datum> list;
private List<AllCategoriesResponse.Datum.SubCategory> categories;
public CategoriesAdapter(Context context, List<AllCategoriesResponse.Datum> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.categories_raw,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
AllCategoriesResponse.Datum items = list.get(position);
if(PrefUser.getLanguage(context).equals("ar"))
holder.textView.setText(items.getArTitle());
else
holder.textView.setText(items.getEnTitle());
SubCategoryAdapter adapter = new SubCategoryAdapter(categories,context);
GridLayoutManager layoutManager = new GridLayoutManager(context,2);
holder.recyclerView.setLayoutManager(layoutManager);
holder.recyclerView.setHasFixedSize(true);
holder.recyclerView.setAdapter(adapter);
}
因為您已初始化但未向其中分配任何列表
private List<AllCategoriesResponse.Datum.SubCategory> categories;
解!
AllCategoriesResponse
模型類的Datum
的make getter方法
public List<SubCategory> getSubCategories(){
return mSubCategories;
}
使用下面的適配器中的方法
SubCategoryAdapter adapter = new SubCategoryAdapter(items.getSubCategories(),context);
問題是您尚未初始化數據或將數據設置到類別列表。
Private List<AllCategoriesResponse.Datum.SubCategory> categories;
caterories= items.getSubCategories();
選擇要在其上調試應用程序的設備。 在代碼中設置斷點。 在運行時檢查變量並評估表達式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.