我正在尝试在另一个回收器 View 中创建一个回收器 View ,并从包含列表列表的 API 获取数据。我成功地在主回收器 View 中显示了数据,但在子回收器 View 中我得到了列表的空大小。
我尝试制作一个主回收器 View ,并在 MainAdapter 中调用了第二个适配器,但我在第二个适配器上得到了空大小,我只需要有人告诉我为什么会收到此错误,尽管它已成功加载数据
API:
{
"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
中创建getter方法
public List<SubCategory> getSubCategories(){
return mSubCategories;
}
在适配器中使用如下所示的方法
SubCategoryAdapter adapter = new SubCategoryAdapter(items.getSubCategories(),context);
关于java - 如何从json中获取列表列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57738243/