无法从CursorWindow读取第0行,第2列。在从游标访问数据之前,请确保已正确初始化游标

时间:2019-06-10 07:00:08

标签: android android-sqlite android-textwatcher

在我的App中,我有一个表格。用户将CNIC放在哪个位置。如果他的CNIC已经存在于SQLite数据库中。用户名和手机号码将自动填充在edittext中(使用TextWatcher)。但是,只要我键入相同的CNIC,应用程序就会崩溃。如果数据库中存在CNIC,则应用程序崩溃,而不是自动填充名称和电话字段。我已经做了很多尝试,但是无法解决我的问题。

查询方法:

public List<SGenerateModel> getGeneratedData(String seller_cnic){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + TABLE_COL2 + ", " + TABLE_COL3 + " FROM " + TABLE_NAME + " WHERE " + TABLE_COL4 + " =?" ;
    Cursor cursor = db.rawQuery(query, new String[]{"" + seller_cnic});

    List<SGenerateModel> listOfSGModel = new ArrayList<>();
    while(cursor.moveToNext()){
        SGenerateModel sGenerateModel = new SGenerateModel(cursor.getString(1), cursor.getString(2));
        sGenerateModel.setId(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_COL1)));
        listOfSGModel.add(sGenerateModel);
    }
    return listOfSGModel;
}

查询方法称为:

   public void generate(){

    if (etSname != null)
        selName = (!etSname.getText().toString().equals("")?
                etSname.getText().toString() : "NULL");

    if (etSphone != null)
        selPhone = (!etSphone.getText().toString().equals("")?
                etSphone.getText().toString() : "NULL");

    if (etScnic != null)
        selCnic = (!etScnic.getText().toString().equals("")?
                etScnic.getText().toString() : "NULL");

    sGenerateModelList = databaseHelper.getGeneratedData(selCnic);
    for (SGenerateModel sGenerateModel : sGenerateModelList){
        selName = sGenerateModel.getSellerName() ;
        etSname.setText(selName);
        selPhone = sGenerateModel.getSellerMobile();
        etSphone.setText(selPhone);
    }
}

TextWatcher代码:

TextWatcher textWatcher2 = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            generate();
        }
    };

etSname.addTextChangedListener(textWatcher2);
    etSphone.addTextChangedListener(textWatcher2);
    etScnic.addTextChangedListener(textWatcher2);

LogCat错误:

 java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.database.CursorWindow.nativeGetString(Native Method)
    at android.database.CursorWindow.getString(CursorWindow.java:438)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
    at com.example.cattleapp.helpers.DatabaseHelper.getGeneratedData(DatabaseHelper.java:218)
    at com.example.cattleapp.activities.MainActivity.generate(MainActivity.java:536)
    at com.example.cattleapp.activities.MainActivity$7.afterTextChanged(MainActivity.java:241)
    at android.widget.TextView.sendAfterTextChanged(TextView.java:9485)
    at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:12080)
    at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1262)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:574)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:507)
    at com.santalu.maskedittext.MaskTextWatcher.format(MaskTextWatcher.kt:67)
    at com.santalu.maskedittext.MaskTextWatcher.afterTextChanged(MaskTextWatcher.kt:21)
    at android.widget.TextView.sendAfterTextChanged(TextView.java:9485)

1 个答案:

答案 0 :(得分:2)

String query = "SELECT " + TABLE_COL2 + ", " + TABLE_COL3 + " FROM " + TABLE_NAME + " WHERE " + TABLE_COL4 + " =?" ;

在这一行中,您仅获取2列(索引0和1),但是在获取时,您尝试获取第2列,即

cursor.getString(2)

您只能获得列0和1。

尝试一下:

SGenerateModel sGenerateModel = new SGenerateModel(cursor.getString(0), cursor.getString(1));

编辑:

使用后始终关闭光标。在您的代码中,在while循环之后,将此行放在方法return listOfSGModel中的getGeneratedData之前

cursor.close();