gpt4 book ai didi

java - Arraylist 不断显示最后一个元素 n 次

转载 作者:行者123 更新时间:2023-12-02 02:59:00 26 4
gpt4 key购买 nike

我创建了一个数组列表来存储从网络服务获取的数据。每次迭代后都会获取数据,但数组列表中仅显示最后一个元素。我尝试了各种解决方案,但对我的情况似乎没有帮助

       public void onResponse(JSONObject paramAnonymousJSONObject)
{
JSONObject localJSONObject;
int j;
try
{
localJSONObject = paramAnonymousJSONObject.getJSONObject("status");
j = localJSONObject.getInt("code");
if (j == 200) {
JSONArray carModelArray = paramAnonymousJSONObject.getJSONArray("carModels");
for (int i = 0; i <= carModelArray.length(); i++) {
JSONObject tariffResponse = null;
JSONObject tariff = carModelArray.getJSONObject(i);
try {
carId = tariff.getString("id");
segment = tariff.getString("segment");
imageURL = tariff.getString("imageURL");
tariffResponse = tariff.getJSONObject("tariffResponse");
CarType = tariffResponse.getString("carModel");
weekdayTariff = tariffResponse.getString("weekdayTariff");
weekendTariff = tariffResponse.getString("weekendTariff");
peakSeasonTariff = tariffResponse.getString("peakSeasonTariff");
maintenanceCharge = tariffResponse.getString("maintainanceFee");
securityDeposite = tariffResponse.getString("securityDeposite");
ArrayList arrayList = TariffActivity.this.tariffModelsList;
arrayList.add(new TariffModel(carId, CarType, imageURL, "\u20b9 " + weekdayTariff, "\u20b9 " + weekendTariff, "\u20b9 " + peakSeasonTariff, "\u20b9 " + securityDeposite, segment, "\u20b9 " + maintenanceCharge));
TariffActivity.this.tariffAdapter = new TariffAdapter(TariffActivity.this.mContext,arrayList);
TariffActivity.this.mRecyclerView.setAdapter(tariffAdapter);
} catch (JSONException e) {
e.printStackTrace();
}

}

最佳答案

执行如下操作。

public void onResponse(JSONObject paramAnonymousJSONObject)
{
JSONObject localJSONObject;
int j;
try
{
localJSONObject = paramAnonymousJSONObject.getJSONObject("status");
j = localJSONObject.getInt("code");
if (j == 200) {
JSONArray carModelArray = paramAnonymousJSONObject.getJSONArray("carModels");
ArrayList arrayList = TariffActivity.this.tariffModelsList;
for (int i = 0; i <= carModelArray.length(); i++) {
JSONObject tariffResponse = null;
JSONObject tariff = carModelArray.getJSONObject(i);
try {
carId = tariff.getString("id");
segment = tariff.getString("segment");
imageURL = tariff.getString("imageURL");
tariffResponse = tariff.getJSONObject("tariffResponse");
CarType = tariffResponse.getString("carModel");
weekdayTariff = tariffResponse.getString("weekdayTariff");
weekendTariff = tariffResponse.getString("weekendTariff");
peakSeasonTariff = tariffResponse.getString("peakSeasonTariff");
maintenanceCharge = tariffResponse.getString("maintainanceFee");
securityDeposite = tariffResponse.getString("securityDeposite");

arrayList.add(new TariffModel(carId, CarType, imageURL, "\u20b9 " + weekdayTariff, "\u20b9 " + weekendTariff, "\u20b9 " + peakSeasonTariff, "\u20b9 " + securityDeposite, segment, "\u20b9 " + maintenanceCharge));

} catch (JSONException e) {
e.printStackTrace();
}

}
TariffActivity.this.tariffAdapter = new TariffAdapter(TariffActivity.this.mContext,arrayList);
TariffActivity.this.mRecyclerView.setAdapter(tariffAdapter);

你必须在for循环之外编写ArrayList赋值,因为如果你在in循环内部编写,那么它每次都会重新初始化,所以你必须添加的元素在之后是清楚的重新初始化。这就是为什么列表中只有最后一项。

关于java - Arraylist 不断显示最后一个元素 n 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42641583/

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