Java 带有customArrayAdapter的ArrayList仅显示1个列表
我使用customArrayAdapter创建了arraylist,在使用模拟数据时,列表在屏幕上是可见的,没有任何错误,但当我用JsonObject替换模拟数据时,json响应在java文件中硬编码为字符串值,然后我发现所有数据都正确显示在列表中,但屏幕上只显示1个列表项,而不是15个,因为响应包含15个JsonArray对象。 我使用for循环在JsonArray中循环,但我不知道错误是什么 MainActivity.Java: MainActivity.java:18行包含:Java 带有customArrayAdapter的ArrayList仅显示1个列表,java,android,json,arraylist,android-arrayadapter,Java,Android,Json,Arraylist,Android Arrayadapter,我使用customArrayAdapter创建了arraylist,在使用模拟数据时,列表在屏幕上是可见的,没有任何错误,但当我用JsonObject替换模拟数据时,json响应在java文件中硬编码为字符串值,然后我发现所有数据都正确显示在列表中,但屏幕上只显示1个列表项,而不是15个,因为响应包含15个JsonArray对象。 我使用for循环在JsonArray中循环,但我不知道错误是什么 MainActivity.Java: MainActivity.java:18行包含: 感谢您发布
感谢您发布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响应中,那么您还需要在这些JSONObject上添加null检查。感谢您发布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响应中,那么您还需要在这些JSONObject上添加空检查。谢谢
我想知道答案。
感谢您指出使用opt而不是get的实际错误。
在修复该错误后,我发现了更多类似的错误,没有分配值,我使用optString、OptObject而不是getString、getJson,并在缺少值时分配了一个默认值
下面是我在readFromJson方法中所做的更改
错误:
publicstaticarraylistreadfromJSON(字符串jsonObject){
ArrayList currentBookStore=新的ArrayList();
if(TextUtils.isEmpty(jsonObject)){
返回null;
}
试一试{
双倍金额=0;
字符串currencyCode=“不可用”;
String textSnippet=“无可用描述”;
JSONObject根=新的JSONObject(JSONObject);
JSONArray items=root.getJSONArray(“items”);
对于(int i=0;i
谢谢
我想知道答案。
感谢您指出使用opt而不是get的实际错误。
在修复该错误后,我发现了更多类似的错误,没有分配值,我使用optString、OptObject而不是getString、getJson,并在缺少值时分配了一个默认值
下面是我在readFromJson方法中所做的更改
错误:
publicstaticarraylistreadfromJSON(字符串jsonObject){
ArrayList currentBookStore=新的ArrayList();
if(TextUtils.isEmpty(jsonObject)){
返回null;
}
试一试{
双倍金额=0;
字符串currencyCode=“不可用”;
String textSnippet=“无可用描述”;
JSONObject根=新的JSONObject(JSONObject);
JSONArray items=root.getJSONArray(“items”);
对于(int i=0;i01-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)
ArrayList<BookStore> bookStoreArrayList = HTTTP_REQUEST.readFromJson(HTTTP_REQUEST.jsonResponse);
JSONObject listPrice = saleInfo.getJSONObject("listPrice");
Double amount = null;
String currencyCode = null;
if(listPrice != null) {
amount = listPrice.getDouble("amount");
currencyCode = listPrice.getString("currencyCode");
}
public static ArrayList<BookStore> readFromJson(String jsonObject) {
ArrayList<BookStore> currentBookStore = new ArrayList<>();
if (TextUtils.isEmpty(jsonObject)) {
return null;
}
try {
double amount=0;
String currencyCode = "Not Available";
String textSnippet = "No Description Available";
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.optString("subtitle");
JSONArray author = volumeInfo.getJSONArray("authors");
String authorName = author.getString(0);
int PageCount = volumeInfo.getInt("pageCount");
JSONObject saleInfo = currentBook.getJSONObject("saleInfo");
JSONObject listPrice =saleInfo.optJSONObject("listPrice");
if(listPrice != null) {
amount = listPrice.getDouble("amount");
currencyCode = listPrice.getString("currencyCode");
}
JSONObject searchInfo = currentBook.optJSONObject("searchInfo");
if (searchInfo!=null){
textSnippet = searchInfo.optString("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;
}