Как из SQLite вывести данные нескольких столбцов в отдельные TextView?

Есть программа с БД в которую я вношу данные: дата, категория, комментарий, сумма. Нужно вывести список всех записей в ListView. На данный момент вообще ничего не выводит, как не пытался максимум что выводилось это либо все данные в один столбец, либо последнее значение в какой нибудь из столбцов... Ниже код главного активити:

package com.trofimoff.notepad;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.SimpleCursorAdapter;
import com.trofimoff.notepad.databinding.ActivityMainBinding;
import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    public ArrayAdapter<String> arrayAdapter;
    private ActivityMainBinding binding;
    public DataBase dataBase;
    public SQLiteDatabase db;
    public Cursor userCursor;
    public SimpleCursorAdapter userAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        dataBase = new DataBase(this);

        load();

        binding.imageButton.setOnClickListener(view -> {
            Intent intent = new Intent("com.trofimoff.notepad.DataFilling");
            startActivity(intent);
        });
    }

    private void load(){
        db = dataBase.getReadableDatabase();
        userCursor = db.query( DataBase.db_table, new String[]{DataBase.db_id, DataBase.db_date, DataBase.db_category, DataBase.db_comment, DataBase.db_sum}, null, null, null, null, null);
        String[] headers = new String[]{DataBase.db_id, DataBase.db_date, DataBase.db_category, DataBase.db_comment, DataBase.db_sum};
        do {
            userAdapter = new SimpleCursorAdapter(this, R.layout.list_row, userCursor, headers, new int[]{R.id.textDate, R.id.textCategory, R.id.textComment, R.id.sum}, 0);
            binding.listView.setAdapter(userAdapter);
        } while (userCursor.moveToFirst());
        userCursor.close();
        db.close();
    }
}

xml файл:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="#f9fbe7"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="533dp" />


    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="66dp"
        android:layout_height="62dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="10dp"
        android:background="@android:color/transparent"
        android:backgroundTint="@color/black"
        android:contentDescription="@string/add_button"
        app:srcCompat="@android:drawable/ic_menu_add"
        tools:ignore="ImageContrastCheck" />
</RelativeLayout>

Класс базы данных:

package com.trofimoff.notepad;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;

public class DataBase extends SQLiteOpenHelper {

    private static final int db_version = 1;
    private static final String db_name = "notepad";
    public static final String db_table = "notepad_list";
    public static final String db_id = "_id";
    public static final String db_date = "date";
    public static final String db_category = "category";
    public static final String db_comment = "comment";
    public static final String db_sum = "sum";

    public DataBase(@Nullable Context context) {
        super(context, db_name, null, db_version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = String.format("CREATE TABLE %s (" + db_id + " ID INTEGER PRIMARY KEY, " +
                        "%s TEXT NOT NULL, %s TEXT NOT NULL, %s TEXT NOT NULL, %s TEXT NOT NULL);",
                db_table, db_date,            db_category,      db_comment,       db_sum);
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        String query = String.format("DELETE TABLE IF EXISTS %s", db_table);
        db.execSQL(query);
        onCreate(db);
    }

    public void insertData(Note note){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(db_date, note.getDate());
        values.put(db_category, note.getCategory());
        values.put(db_comment, note.getComment());
        values.put(db_sum, note.getSum());
        db.insertWithOnConflict(db_table, null, values, SQLiteDatabase.CONFLICT_REPLACE);
    }
}

xml с текстами который должен выводится в ListView на главном окне.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textDate"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginEnd="5dp"
        android:layout_weight="1"
        android:background="@drawable/rounding"
        android:text="@string/date"
        android:textAlignment="center"
        android:textColor="#33691E"
        android:textSize="16sp"
        app:autoSizeTextType="none" />

    <TextView
        android:id="@+id/textCategory"
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:layout_marginEnd="5dp"
        android:layout_weight="1"
        android:background="@drawable/rounding"
        android:text="@string/category"
        android:textAlignment="center"
        android:textColor="#33691E"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textComment"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_marginEnd="5dp"
        android:layout_weight="1"
        android:background="@drawable/rounding"
        android:text="@string/comment"
        android:textAlignment="center"
        android:textColor="#33691E"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/sum"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:layout_marginEnd="5dp"
        android:layout_weight="1"
        android:background="@drawable/rounding"
        android:text="@string/sum"
        android:textAlignment="center"
        android:textColor="#33691E"
        android:textSize="16sp" />
</LinearLayout>

Ответы (1 шт):

Автор решения: Mr WalAlex

Корректный метод:

    private void load(){
    db = dataBase.getReadableDatabase();
    userCursor = db.query( DataBase.db_table, new String[]{DataBase.db_id, DataBase.db_date, DataBase.db_category, DataBase.db_comment, DataBase.db_sum}, null, null, null, null, null);
    String[] headers = new String[]{DataBase.db_date, DataBase.db_category, DataBase.db_comment, DataBase.db_sum};
    userAdapter = new SimpleCursorAdapter(this, R.layout.list_row, userCursor, headers, new int[]{R.id.textDate, R.id.textCategory, R.id.textComment, R.id.sum}, 0);
    binding.listView.setAdapter(userAdapter);
}
→ Ссылка