java - 如何从json中获取列表列表?

标签 java android android-recyclerview retrofit

我正在尝试在另一个回收器 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/

相关文章:

java - 圆计算器有错误

java - 错误:“XLConnectJars”的程序包或 namespace 加载失败

android - 将 EditText 值转换为 double /整数并在 Android 中执行数学函数时遇到问题

android - 在回收站 View 中过滤结果为 0 时显示空状态

java - 如果我从 root pom 的命令行运行插件,它在遍历模块时会做什么?

java - 如何通过接口(interface)使用 Java ENUM?

java - 安卓:java.lang.NoClassDefFoundError

Android 每 2 小时以编程方式将 GPS 发送到 MySql 数据库

android - 转动模拟器后布局发生变化,但不是设备

android - 子 Recyclerview 中的项目自动滚动