Can someone help me with how to add my JSON values to the list? I can't seem to insert it despite putting List.add() function. It's my first time on tinkering JSON stuff in Android. I really appreciate the help.
P.S: somehow, the "API CALL SUCCESS" toast was triggering but the values that I extract won't add in the List.
UPDATE: Still can't find a fitting implementation.
fun createDataset(): ArrayList<ItemPost>{
val url = "http://api.karawcraftventure.com/item"
val list = ArrayList<ItemPost>()
val Queue = Volley.newRequestQueue(activity)
val jsonObject = JsonArrayRequest(
Request.Method.GET,url,null,
{response ->
Toast.makeText(context, "API CALL SUCCESS", Toast.LENGTH_SHORT).show()
try
{
for (i in 0 until response.length())
{
val item : JSONObject = response.getJSONObject(i)
val API_Image : String = item.getString("product_image").
val API_ItemName : String = item.getString("product_name")
val API_Price : String = item.getString("product_price")
val API_Category : String = item.getString("product_category")
list.add(
ItemPost(
API_Image,
API_ItemName,
API_Category,
API_Price
)
)
}
}
catch (e: JSONException)
{
e.printStackTrace()
}
},
{ error: VolleyError? -> Toast.makeText(context, error?.message.toString(), Toast.LENGTH_SHORT).show()
}
)
Queue.add(jsonObject)
return list
}