1

If you look at the code below the ENERC_KCAL,FAT,FASAT,FAMS all of them has the same 3 parameters label, quantity and unit. I want to set the label and quantity value in my RecyclerAdapter to populate a recylerView.

"totalNutrients":{
   "ENERC_KCAL":{
      "label":"Energy",
      "quantity":2493.0949190757847,
      "unit":"kcal"
   },
   "FAT":{
      "label":"Fat",
      "quantity":2.275,
      "unit":"g"
   },
   "FASAT":{
      "label":"Saturated",
      "quantity":0.0665,
      "unit":"g"
   },
   "FAMS":{
      "label":"Monounsaturated",
      "quantity":0.224,
      "unit":"g"
   }
}

However, in my data class, is creating individual objects of type ENERC_KCAL,FAT,FASAT,FAMS(I'm using the Json to Kotlin Class Plugin) in the following format



                    data class ENERCKCAL(
                    @SerializedName("label")
                    val label: String?,
                    @SerializedName("quantity")
                    val quantity: Double?,
                    @SerializedName("unit")
                    val unit: String?
                )

                data class FAT(
                    @SerializedName("label")
                    val label: String?,
                    @SerializedName("quantity")
                    val quantity: Double?,
                    @SerializedName("unit")
                    val unit: String?
                )

                data class FASAT(
                    @SerializedName("label")
                    val label: String?,
                    @SerializedName("quantity")
                    val quantity: Double?,
                    @SerializedName("unit")
                    val unit: String?
                )

Is there a way to generate a GenericClass to access the label and quantity value or how do i access the values in the present structure?

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31

2 Answers2

3

If you wish to display your data in recyclerview, try this:

  data class TotalNutrients(@SerializedName("ENERC_KCAL")val enerc:Nutrient, @SerializedName("FAT")val fat:Nutrient, @SerializedName("FASAT")val fasat:Nutrient, @SerializedName("FAMS")val fams:Nutrient) {

        data class Nutrient(@SerializedName("label")
                            val label: String?,
                            @SerializedName("quantity")
                            val quantity: Double?,
                            @SerializedName("unit")
                            val unit: String?
        )
        
        fun getArrayList():ArrayList<Nutrient>{
            return arrayListOf(enerc,fat,fasat,fams)
        }

    }

Now you can use getArrayList() to populate a recyclerview

Pavel B.
  • 805
  • 10
  • 13
  • This works. However, in my project I have 45 elements just like ENERC_KCAL, etc. is there any alternative? – Narendra_Nath May 02 '21 at 17:08
  • perhaps smth like this: https://stackoverflow.com/questions/16295949/get-all-fields-even-private-and-inherited-from-class – Pavel B. May 02 '21 at 17:19
0

There is a better solution to the same problem in case you have a lot of similar data classes.

data class Nutrient(@SerializedName("label")
                            val label: String?,
                            @SerializedName("quantity")
                            val quantity: Double?,
                            @SerializedName("unit")
                            val unit: String?
        )

and in the upper data class define this as a

val valuesOfNutrients:Map<String, Nutrient>

then you can access all the elements using valuesOfNutrients.value without the need for the intermediate data classes

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31