【发布时间】:2020-05-10 13:06:45
【问题描述】:
因此,我遵循了有关 sqlite 和 android studio 以及如何将数据保存到数据库中以及以后能够查看这些数据的教程。本教程仅展示了如何将一个值保存到数据库中。但是如何将多个值保存到这个数组中呢?
这是将数据添加到数组中的方法:
private void populateListView() {
Log.d(TAG, "populateListView: Displaying data in the ListView.");
Cursor data = mDatabaseHelper.getData();
ArrayList<String> listData = new ArrayList<>();
while(data.moveToNext()){
listData.add("Id :"+ data.getString(0)+"\n");
listData.add("Value :"+ data.getString(1)+"\n");
listData.add("Note :"+ data.getString(2)+"\n");
listData.add("Category :"+ data.getString(3)+"\n");
listData.add("Payment :"+ data.getString(4)+"\n\n");
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);
只要我尝试向 Listview 添加多个值,应用就会崩溃。 这是我的列表视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我是否必须在此处更改任何内容才能添加多个值?我对 android stuido 和 java 完全陌生,所以可能只有一个愚蠢的错误我看不到。
【问题讨论】:
标签: java android sql sqlite android-sqlite