简体   繁体   中英

Acess nested JSON data in recyclerview adapter using Retrofit

I am using Retrofit2.4.0 to get the data from mysql database which uses PHP as webservice. I return JSON data in the following format :-

{
"result": [
    {
        "category_Id": 1,
        "category_Name": "Jyotirling",
        "category_Image": "jyotirling.jpg",
        "total_Mandir": 12
    },
    {
        "category_Id": 2,
        "category_Name": "Akshardham",
        "category_Image": "akshardham.jpg",
        "total_Mandir": 2
    },
    {
        "category_Id": 3,
        "category_Name": "AshtaVinayak",
        "category_Image": "ashtavinayak.jpg",
        "total_Mandir": 8
    }
],
"message": "Data found",
"status": "200"
}

My Pojo Classes are

1)

public class MandirCategory {

    @SerializedName("result")
    @Expose
    private List<MandirCategoryResults> result = null;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("status")
    @Expose
    private String status;

    public List<MandirCategoryResults> getResult() {
        return result;
    }

    public void setResult(List<MandirCategoryResults> result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

}

2) The result class for nested data

public class MandirCategoryResults {
    @SerializedName("category_Id")
    @Expose
    private Integer categoryId;
    @SerializedName("category_Name")
    @Expose
    private String categoryName;
    @SerializedName("category_Image")
    @Expose
    private String categoryImage;
    @SerializedName("total_Mandir")
    @Expose
    private Integer totalMandir;

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public String getCategoryImage() {
        return categoryImage;
    }

    public void setCategoryImage(String categoryImage) {
        this.categoryImage = categoryImage;
    }

    public Integer getTotalMandir() {
        return totalMandir;
    }

    public void setTotalMandir(Integer totalMandir) {
        this.totalMandir = totalMandir;
    }
}

But now I am lost as to how to access the data of a nested class in a recyclerview.

I can access message attribute in the recyclerview like this :

String msg = listData.get(i).getMessage();

But how to get value of categoryName ?

EDIT : Code that is used to get the JSON Data

 @GET("getMandirCategories.php")
 Call<List<MandirCategory>> getMandirCategoriesREST();

Are you trying to present the List with an RecyclerView ? If not, assuming the parsing is correct, this should work (indexMandirCategory represents the index in List):

String category = listData.get(i).getResult().get(indexMandirCategory).getCategoryName();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM