Я пытаюсь получить приведенные ниже объекты JSONArray.Есть 2 элемента, но с помощью цикла, который я реализовал, я могу получить только 1 элемент.Я использовал 2 для петель.Как это решить?Есть ли другой способ получить все данные?Если я запускаю цикл for с -1, тогда он показывает индекс массива вне границ.
Структура JSON такая, как показано ниже
{
"status": 200,
"list": [
{
"quot_uid": "QUOTE2018@1",
"id": "1",
"expiry_date": "2018-05-29",
"created_at": "2018-05-22 11:45:58",
"left_days": "9",
"items": [
{
"ITEM_NAME": "Copper Wires",
"UNIT": "MT",
"qty": "5",
"make": null
},
{
"ITEM_NAME": "OFC Cables",
"UNIT": "MT",
"qty": "2",
"make": null
}
]
}
]
}
И это код, который я уже пробовал
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("list");
tv.removeAllViewsInLayout();
int flag = 1;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONArray jsonArray1 = jsonObject1.getJSONArray("items");
for (int j = 0; j < jsonArray1.length(); j++) {
TableRow tr = new TableRow(Main2Activity.this);
tr.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView t1 = new TextView(Main2Activity.this);
t1.setPadding(10, 30, 10, 30);
t1.setText("ITEM NAME");
t1.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
t1.setTop(20);
t1.setTextColor(Color.BLACK);
t1.setTextSize(15);
t1.setTypeface(null, Typeface.BOLD);
tr.addView(t1);
TextView t2 = new TextView(Main2Activity.this);
t2.setPadding(10, 30, 10, 30);
t2.setText("QTY");
t2.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
t2.setTop(20);
t2.setTextColor(Color.BLACK);
t2.setTextSize(15);
t2.setTypeface(null, Typeface.BOLD);
tr.addView(t2);
tv.addView(tr);
final View vline = new View(Main2Activity.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 10));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
} else {
JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
TextView tv1 = new TextView(Main2Activity.this);
tv1.setPadding(5, 30, 5, 30);
String item_nm = jsonObject2.getString("ITEM_NAME");
tv1.setText(item_nm);
tv1.setTextColor(Color.BLACK);
tv1.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
tv1.setTextSize(15);
tr.addView(tv1);
TextView tv2 = new TextView(Main2Activity.this);
tv2.setPadding(5, 30, 5, 30);
String item_qty = jsonObject2.getString("qty");
tv2.setText(item_qty);
tv2.setTextColor(Color.BLACK);
tv2.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
tv2.setTextSize(15);
tr.addView(tv2);
tv.addView(tr);
final View vline1 = new View(Main2Activity.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.rgb(1, 132, 143));
tv.addView(vline1);
}