我陷入一个循环状态,我在另一个循环中使用for循环,以便在我从Json响应中获取的嵌套recyclerview中显示数据,但没有得到所需的解决方案,下面是我的代码,我也正在附加我的结果输出并附加了必需的输出,如果有人知道解决方案,请帮助我。
我的代码在这里
JSONArray jsonArray = jsonRootObject.getJSONArray("data");
for(int i=0;i < jsonArray.length();i++){
JSONObject object1 =jsonArray.getJSONObject(i);
JSONArray jsonArrayRates = object1.getJSONArray("rates");
Log.e("jsonArraySize",String.valueOf(jsonArrayRates.length()));
String fuel_type = null;
demoCarSectionList.clear();
for (int j=0;j < jsonArrayRates.length();j++){
JSONObject objectRates=jsonArrayRates.getJSONObject(j);
access_Rate=objectRates.getString("access_rate");
String carID=objectRates.getString("car_id");
Log.e( "carIdInside" ,carID);
amount= (rate_int * time_int);
freeDriving= String.valueOf(distance_Car);
demoCarSectionList.add(access_Rate);
}
Log.e( "value_inside" , demoCarSectionList.get(1));
demoCarList.add((ArrayList<String>) demoCarSectionList);
}
Resulted output from above code-
E/RateList: [12, 11.5, 11]
E/carList: [[12, 11.5, 11]]
E/RateList: [14, 10, 15]
E/carList: [[14, 10, 15], [14, 10, 15]]
E/RateList: [14, 13, 12]
E/carList: [[14, 13, 12], [14, 13, 12], [14, 13, 12]]
E/RateList: [14, 18, 19]
E/carList: [[14, 18, 19], [14, 18, 19], [14, 18, 19], [14, 18, 19]]
E/RateList: [15, 19, 10]
E/carList: [[15, 19, 10], [15, 19, 10], [15, 19, 10], [15, 19, 10],
[15, 19, 10]]
E/RateList: [14, 16, 12]
E/carList: [[14, 16, 12], [14, 16, 12], [14, 16, 12], [14, 16, 12],
[14, 16, 12], [14, 16, 12]]
E/RateList: [14, 13, 12]
E/carList: [[14, 13, 12], [14, 13, 12], [14, 13, 12], [14, 13, 12],
[14, 13, 12], [14, 13, 12], [14, 13, 12]]
E/RateList: [15, 14, 13]
E/carList: [[15, 14, 13], [15, 14, 13], [15, 14, 13], [15, 14, 13],
[15, 14, 13], [15, 14, 13], [15, 14, 13], [15, 14, 13]]
E/RateList: [13, 12, 11]
E/carList: [[13, 12, 11], [13, 12, 11], [13, 12, 11], [13, 12, 11],
[13, 12, 11], [13, 12, 11], [13, 12, 11], [13, 12, 11], [13, 12, 11]]
E/RateList: [14, 13, 12]
E/carList: [[14, 13, 12], [14, 13, 12], [14, 13, 12], [14, 13, 12],
[14, 13, 12], [14, 13, 12], [14, 13, 12], [14, 13, 12], [14, 13, 12],
[14, 13, 12]]
E/RateList: [13, 12, 10]
E/carList: [[13, 12, 10], [13, 12, 10], [13, 12, 10], [13, 12, 10],
[13, 12, 10], [13, 12, 10], [13, 12, 10], [13, 12, 10], [13, 12, 10],
[13, 12, 10], [13, 12, 10]]
Required output-
E/RateList: [12, 11.5, 11]
E/carList: [[12, 11.5, 11]]
E/RateList: [14, 10, 15]
E/carList: [[12, 11.5, 11], [14, 10, 15]]
E/RateList: [14, 13, 12]
E/carList: [[12, 11.5, 11], [14, 10, 15], [14, 13, 12]]
E/RateList: [14, 18, 19]
E/carList: [[12, 11.5, 11], [14, 10, 15], [14, 13, 12], [14, 18, 19]]
E/RateList: [15, 19, 10]
E/carList: [[12, 11.5, 11], [14, 10, 15], [14, 13, 12], [14, 18, 19],
[15, 19, 10]]
E/RateList: [14, 16, 12]
E/carList: [[12, 11.5, 11], [14, 10, 15], [14, 13, 12], [14, 18, 19],
[15, 19, 10], [14, 16, 12]]
E/RateList: [14, 13, 12]
E/carList: [[12, 11.5, 11], [14, 10, 15], [14, 13, 12], [14, 18, 19],
[15, 19, 10], [14, 16, 12], [14, 13, 12]]
E/RateList: [15, 14, 13]
E/carList: [[12, 11.5, 11], [14, 10, 15], [14, 13, 12], [14, 18, 19],
[15, 19, 10], [14, 16, 12], [14, 13, 12], [15, 14, 13]]
E/RateList: [13, 12, 11]
E/carList: [[12, 11.5, 11], [14, 10, 15], [14, 13, 12], [14, 18, 19],
[15, 19, 10], [14, 16, 12], [14, 13, 12], [15, 14, 13], [13, 12, 11]]
E/RateList: [14, 13, 12]
E/carList: [[12, 11.5, 11], [14, 10, 15], [14, 13, 12], [14, 18, 19],
[15, 19, 10], [14, 16, 12], [14, 13, 12], [15, 14, 13], [13, 12, 11],
[14, 13, 12]]
E/RateList: [13, 12, 10]
E/carList: [[12, 11.5, 11], [14, 10, 15], [14, 13, 12], [14, 18, 19],
[15, 19, 10], [14, 16, 12], [14, 13, 12], [15, 14, 13], [13, 12, 11],
[14, 13, 12], [13, 12, 10]]
答案 0 :(得分:0)
经过2-3天的努力后,我得到了答案,这都与列表的初始化有关,它的很小的错误,demoCarSectionList需要在本地初始化,因此它将为循环的每个索引更新列表。
代码应该像这样
JSONArray jsonArray = jsonRootObject.getJSONArray("data");
for(int i=0;i < jsonArray.length();i++){
JSONObject object1 =jsonArray.getJSONObject(i);
JSONArray jsonArrayRates = object1.getJSONArray("rates");
Log.e("jsonArraySize",String.valueOf(jsonArrayRates.length()));
String fuel_type = null;
demoCarSectionList= new ArrayList<>();
demoCarSectionList.clear();
for (int j=0;j < jsonArrayRates.length();j++){
JSONObject objectRates=jsonArrayRates.getJSONObject(j);
access_Rate=objectRates.getString("access_rate");
String carID=objectRates.getString("car_id");
Log.e( "carIdInside" ,carID);
amount= (rate_int * time_int);
freeDriving= String.valueOf(distance_Car);
demoCarSectionList.add(access_Rate);
}
Log.e( "value_inside" , demoCarSectionList.get(1));
demoCarList.add((ArrayList<String>) demoCarSectionList);
}