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"
}
]
}
Предполагается, что заголовок + "автор" + автор в представлении списка.