【发布时间】:2020-05-11 01:30:50
【问题描述】:
我已经使用 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);
HTTP_REQUEST.java:58 行包含:
JSONObject listPrice = saleInfo.getJSONObject("listPrice");
【问题讨论】:
-
你能贴出
BookAdapter类的代码吗? -
我认为书籍适配器类中没有错误,因为代替我创建的 json 响应: ArrayList
bookStoreArrayList = new ArrayList(); bookStoreArrayList.add(new BookStore("asdf","asdf","asdf","asdf",321,443.00,"asdf"));然后屏幕上显示了一系列列表项 -
您确定您的视图没有包装内容或其他内容,并且它使用匹配父项。可能是它只显示第一个项目,其余部分在滚动中可用。并确保您没有将获取项目大小返回为 1 的适配器类
-
既然您说“发现所有数据都正确显示在列表中,但屏幕上仅显示 1 个列表项”,我只能假设这是适配器问题。
-
@vikaskumar 感谢您的回复,视图在匹配父级中,当我使用此模拟数据时: ArrayList
bookStoreArrayList = new ArrayList(); bookStoreArrayList.add(new BookStore("asdf","asdf","asdf","asdf",321,443.00,"asdf"));然后所有列表项都显示在屏幕上
标签: java android json arraylist android-arrayadapter