java - 带有 customArrayAdapter 的 ArrayList 仅显示 1 个列表
问题描述
我已经使用 customArrayAdapter 创建了arraylist,并且在使用模拟数据时,该列表在屏幕上可见,没有任何错误,但是当我用 JsonObject 替换模拟数据时,其中 json 响应被硬编码为 java 文件中的字符串值,然后我找到了所有数据在列表中正确显示,但屏幕中仅显示 1 个列表项而不是 15 个,因为响应包含 15 个 JsonArray 对象。我已经使用 for 循环遍历 JsonArray 但我不知道错误是什么?
MainActivity.Java :
package com.example.anybookinfo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView bookListView = findViewById(R.id.bookListView);
ArrayList<BookStore> bookStoreArrayList = HTTTP_REQUEST.readFromJson(HTTTP_REQUEST.jsonResponse);
BookAdapter bookAdapter = new BookAdapter(this, bookStoreArrayList);
bookListView.setAdapter(bookAdapter);
}
}
HTTP_Request.java:
package com.example.anybookinfo;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public final class HTTTP_REQUEST {
public static ArrayList<BookStore> readFromJson(String jsonObject) {
ArrayList<BookStore> currentBookStore = new ArrayList<>();
if (TextUtils.isEmpty(jsonObject)) {
return null;
}
try {
JSONObject root = new JSONObject(jsonObject);
JSONArray items = root.getJSONArray("items");
for (int i = 0; i < items.length(); i++) {
JSONObject currentBook = items.getJSONObject(i);
JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");
String fTitle = volumeInfo.getString("title");
String lTitle = volumeInfo.getString("subtitle");
JSONArray author = volumeInfo.getJSONArray("authors");
String authorName = author.getString(0);
int PageCount = volumeInfo.getInt("pageCount");
JSONObject saleInfo = currentBook.getJSONObject("saleInfo");
JSONObject listPrice = saleInfo.getJSONObject("listPrice");
double amount = listPrice.getDouble("amount");
String currencyCode = listPrice.getString("currencyCode");
JSONObject searchInfo = currentBook.getJSONObject("searchInfo");
String textSnippet = searchInfo.getString("textSnippet");
BookStore bookStore = new BookStore(fTitle, lTitle, authorName, textSnippet, PageCount, amount, currencyCode);
currentBookStore.add(bookStore);
}
} catch (JSONException j) {
Log.e("readFromJson Error", "error in reading the json response", j);
}
return currentBookStore;
}
public static final String jsonResponse = " //the json response code is here " ;
}
硬编码的实际 json 响应链接: https ://www.googleapis.com/books/v1/volumes?q=Inner%20Engineering%20:%20a%20yogi%27s%20guide%20to%20joy&maxResults=15
LogCat 错误:
01-24 23:18:46.030 3693-3703/? E/art: Failed sending reply to debugger: Broken pipe
01-24 23:18:46.243 3693-3693/? E/readFromJson Error: error in reading the json response
org.json.JSONException: No value for listPrice
at org.json.JSONObject.get(JSONObject.java:389)
com.example.anybookinfo.HTTTP_REQUEST.readFromJson(HTTTP_REQUEST.java:58)
at com.example.anybookinfo.MainActivity.onCreate(MainActivity.java:18)
MainActivity.java:18 行包含:
ArrayList<BookStore> bookStoreArrayList = HTTTP_REQUEST.readFromJson(HTTTP_REQUEST.jsonResponse);
HTTTP_REQUEST.java:58 行包含:
JSONObject listPrice = saleInfo.getJSONObject("listPrice");
解决方案
感谢您发布 logcat 错误。这是您的项目未在屏幕上显示的原因。查看您提供的 json 数据后,您需要listPrice
在处理它之前进行空检查,因为其中的某些项目saleInfo
没有该字段。所以代替这一行:
JSONObject listPrice = saleInfo.getJSONObject("listPrice");
做:
JSONObject listPrice = saleInfo.optJSONObject("listPrice");
然后做一个空检查listPrice
:
Double amount = null;
String currencyCode = null;
if(listPrice != null) {
amount = listPrice.getDouble("amount");
currencyCode = listPrice.getString("currencyCode");
}
无需添加另一个内部 try/catch。此外,您需要使用Double
该类,因为有可能具有空值,并且还因为您的Bookstore
构造函数正在传递该字段。
如果有其他字段可能不在 json 响应中,那么您还需要对这些 JSONObjects 添加空检查。
推荐阅读
- azure-devops - 使用 Azure Pipelines 将用户名和密码添加到 yaml 配置文件
- go - 服务器关闭请求流时如何关闭 HTTP/2 客户端流?
- jquery - 如何通过 $.post 方法发送查询参数?
- mysql - 带有日期过滤器但使用系统年份的 SQL 查询
- cluster-analysis - 为 Kmeans 聚类选择 K 值
- window - 在 Windows 10 中找不到 Inf2Cat
- swift - 让听众知道何时在 dynamodb 中更新了令人兴奋的项目
- asp.net-core-webapi - LINQ 查询仅选择内部列表(JSON 响应中的问题)
- azure - 如何将标签添加到在 Microsoft Azure 中创建的文件存储、队列存储和表存储
- python - 如何在使用 python 的不同客户的特定月数后计算出平均值?