gpt4 book ai didi

java - 获取 JSON 并在 ListView 上显示,并使用数组适配器强制关闭

转载 作者:行者123 更新时间:2023-12-02 04:01:33 24 4
gpt4 key购买 nike

java运行并能够获取JSON,但无法在布局上显示并强制关闭。我不知道出了什么问题。我猜这是布局上的问题,但我不确定......

我已经在java中添加了日志,它显示java运行到最后并在logcat上显示我需要的所有内容。但它只是没有显示在布局上,之后应用程序被迫关闭。

public class MainActivity extends AppCompatActivity {

private ListView bookList;
private LinearLayout linearLayout;
private String file = "books.json";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

bookList = findViewById(R.id.bookList);

String jsonStr_books = readAssetsFile(file);
ArrayList books = jsonToArrayList_books(jsonStr_books);
Log.v("myLog", "arrayList: " + books);

ArrayAdapter<Book> arrayAdapter = new ArrayAdapter<Book>(
this, R.layout.content_main, books);

bookList.setAdapter(arrayAdapter);
displayBookList(books);
Log.v("myLog", "done");

}

public class Book {
public String title;
public String author;

public String toString(){
return title + " by " + author;
}
}

public String readAssetsFile(String file) {
String json = null;
try {
InputStream is = getAssets().open(file);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}

public ArrayList<Book> jsonToArrayList_books(String jsonStr){
JSONObject jsonObjTable;

ArrayList<Book> arrayList = new ArrayList<>();
try{
jsonObjTable = new JSONObject(jsonStr);
JSONArray jsonArrayTable = jsonObjTable.getJSONArray("books");

for (int i=0; i<jsonArrayTable.length();i++){
JSONObject jsonObjRow = jsonArrayTable.getJSONObject(i);
Book book = new Book();

book.title = jsonObjRow.getString("title");
book.author = jsonObjRow.getString("author");

arrayList.add(book);

}

}catch(JSONException e){
Log.v("myLog", e.toString());
}
return arrayList;
}

private void displayBookList(ArrayList<Book> arrayList){
String bookRow;

for (int i = 0; i<arrayList.size();i++){
bookRow = arrayList.get(i).toString();
TextView tv = new TextView(getApplicationContext());
tv.setText(bookRow);
linearLayout = findViewById(R.id.bookListLayout);
linearLayout.addView(tv);
Log.v("myLog", i + "> " + bookRow);
}
}


}

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main"
android:id="@+id/bookListLayout"
android:orientation="vertical"
>

<ListView
android:id="@+id/bookList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

这是 JSON:

"books":[
{
"id": "1",
"title": "test",
"author": "123"
},
{
"id": "2",
"title": "A Tale of Two Cities",
"author": "Charles Dickens"
},
{
"id": "3",
"title": "Alice in Wonderland",
"author": "Lewis Carroll"
}
]
}

它应该在 ListView 上显示标题 + "by "+ 作者。

最佳答案

从java代码中删除LinearLayout,并且您不需要displayBookList方法也删除它。您已经将适配器传递给 ListView 。

关于java - 获取 JSON 并在 ListView 上显示,并使用数组适配器强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56731677/

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