gpt4 book ai didi

java - 嵌套 RecyclerView 的列表内列表

转载 作者:行者123 更新时间:2023-12-02 03:07:04 25 4
gpt4 key购买 nike

我陷入了一个循环条件,我在另一个 for 循环中使用 for 循环,以在嵌套的 recyclerview 中显示数据,我从 Json 响应中获取该数据,但我没有得到所需的解决方案,下面是我的代码,我还附加了我的结果输出并附加了所需的输出,如果有人知道解决方案,请帮助我。

我的代码在这里

 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]]

最佳答案

经过 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);

}

关于java - 嵌套 RecyclerView 的列表内列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57018241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com