使用sqlite和android studio将多个值保存到一个数组中
Saving multiple values into an array using sqlite and andorid studio
所以我遵循了有关 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);
我一尝试向列表视图添加多个值,应用程序就崩溃了。
这是我的列表视图
<?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 的新手,所以可能只有一个我看不到的愚蠢错误。
不是将每一列作为新项目添加到列表中,而是将每一行的所有列值连接起来并将其添加到列表中,因此列表中的每一项都包含 table 的 1 行。
因此,将 while
循环更改为:
String row;
while(data.moveToNext()){
row = "Id :"+ data.getString(0)+"\n" +
"Value :"+ data.getString(1)+"\n"+
"Note :"+ data.getString(2)+"\n"+
"Category :"+ data.getString(3)+"\n"+
"Payment :"+ data.getString(4);
listData.add(row)
}
所以我遵循了有关 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);
我一尝试向列表视图添加多个值,应用程序就崩溃了。 这是我的列表视图
<?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 的新手,所以可能只有一个我看不到的愚蠢错误。
不是将每一列作为新项目添加到列表中,而是将每一行的所有列值连接起来并将其添加到列表中,因此列表中的每一项都包含 table 的 1 行。
因此,将 while
循环更改为:
String row;
while(data.moveToNext()){
row = "Id :"+ data.getString(0)+"\n" +
"Value :"+ data.getString(1)+"\n"+
"Note :"+ data.getString(2)+"\n"+
"Category :"+ data.getString(3)+"\n"+
"Payment :"+ data.getString(4);
listData.add(row)
}