This is my json :
{
"posts": [
{
"id": "c200",
"title": "erfan",
"email": "[email protected]",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"attachments":[
{
"url":"http://memaraneha.ir/wp-content/uploads/2016/11/light.jpg"
}]},
{
"id": "c201",
"name": "Johnny Depp",
"email": "[email protected]",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"attachments":[
{
"url":"http://memaraneha.ir/wp-content/uploads/2016/11/renovate-old-home.jpg"}]
}]
}
I want to get the title and url inside attachments array. I've tried this so far.
try {
JSONObject jsonObj = new JSONObject(jsonStr);
jsonContent jsonContent = new jsonContent(); //my class
JSONArray posts = jsonObj.getJSONArray("posts");
for (int i = 0; i < posts.length(); i++) {
JSONObject c = posts.getJSONObject(i);
jsonContent.title = c.getString("title");
JSONArray attachments=c.getJSONArray("attachments");
for (int j=0;j<attachments.length();j++) {
JSONObject a=attachments.getJSONObject(j);
jsonContent.imgurl=a.getString("url");
}
listcontent.add(jsonContent); //mylist
}
} catch(Exception e) {}
Then I show the title and the url in a TextView , ImageView inside a RecyclerView. In card 1, I show the first title and image from the parsed json which works fine. But the problem is in card 2, which shows the title and image of card1 and doesn't matter how many json data we have, it just shows that same title and image in all items of my RecyclerView.
Here's the POJO for parsing the Json response.
public class jsonContent {
public String title;
public String imgurl;
}
listcontentis, but unless theaddmethod makes a copy of its argument, you need a newjsonContentobject for each post element. You're adding the same object for every post.jsonContent jsonContent = new jsonContent();to inside theforloop. Have you tried that?