Sqlite 数据库将数据放在错误的列中

Sqlite databse puts data in the wrong columns

当我尝试从模拟器添加数据时,我的数据库出现问题,它把它放在了错误的列中。我知道它是从 0 开始的,比如 (1 column = 0, 2 column = 1....)

我想我将它设置为 1 以将数据放在 2 列中,但它却将它放在 3 列中,但是当将其设置为“0”时,它会像预期的那样在第一列中添加数据。有人知道为什么会发生这种情况吗?

package com.example.laivumusis;

import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class ViewListData extends AppCompatActivity {

    KlausimynoDatabaseHelper myDB;

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

        ListView listView = (ListView) findViewById(R.id.listView);
         myDB = new KlausimynoDatabaseHelper(this);

        ArrayList<String> theList = new ArrayList<>();
        Cursor data = myDB.getListContents();

        if (data.getCount() == 0) {
            Toast.makeText(ViewListData.this,"Database tuščias!",Toast.LENGTH_LONG).show();
        }else{
            while (data.moveToNext()){
                theList.add(data.getString(1));
                ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,theList);
                listView.setAdapter(listAdapter);
            }
        }
    }
}

不要像这样使用列的索引:

theList.add(data.getString(1));

通过传递列名使用方法 getColumnIndex(),因为列的顺序可能与您认为的不同。
所以改为:

theList.add(data.getString(data.getColumnIndex("columnName")));

columnName 替换为您想要的列的名称。