如何在 recyclerview 适配器中填充此数据?

How to populate this data in recyclerview adapter?

我知道如何处理这种回应

[
        {
            “id": "1027",
            “name”: “abcd”,
            "age”: “30”
         },
        {
            “id”: "713",
            "name": "xyz”,
            “age”: “31”
        }
]

我是这样做的:

Call<List<info>> call = apiInterface.getInfo(id);
            call.enqueue(new Callback<List<info>>() {

                @Override
                public void onResponse(Call<List<info>> call, Response<List<info>> response) {

                    myList = response.body();
                    myAdapter = new coursesAdapter(myList, context);
                    recyclerview.setAdapter(myAdapter);
                }

                @Override
                public void onFailure(Call<List<info>> call, Throwable t) {
                    Toast.makeText(context, t.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

但这是我得到的 json 响应:

{
    "catalogue": [
        {
            “id": "1027",
            “name”: “abcd”,
            "age”: “30”
         },
        {
            “id”: "713",
            "name": "xyz”,
            “age”: “31”
        }
]
}

如何在我的 recyclerview 中填充此响应,或者,我如何删除目录并直接访问我的 recyclerview 的内部列表。

任何帮助将不胜感激和认可。谢谢

您的适配器将保持不变。你只需要改变解析逻辑。

Call<YourObj> call = apiInterface.getInfo(id);
    call.enqueue(new Callback<YourObj>() {

        @Override
        public void onResponse(Call<YourObj> call, Response<YourObj> response) {

            YourObj reponse = response.body();
            myAdapter = new coursesAdapter(reponse.info, context);
            recyclerview.setAdapter(myAdapter);
        }

        @Override
        public void onFailure(Call<List<info>> call, Throwable t) {
            Toast.makeText(context, t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
    
            

您的 Pojo class 应该如下所示,根据您的要求对您的 pojo class 进行必要的更改。

public class Info {

    @SerializedName("id")
    public String id;
    
    @SerializedName("name")
    public String name;
    
    @SerializedName("age")
    public String age;

}


public class YourObj {

    @SerializedName("catalogue")
    public List<Info> info = null;
}