如何通过获取SQLite数据自动填充编辑文本?

时间:2019-06-11 06:54:28

标签: android android-edittext 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(0), cursor.getString(1));
        listOfSGModel.add(sGenerateModel);
    }
    cursor.close();
    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:

android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=939 (# cursors opened by this proc=939)
    at android.database.CursorWindow.<init>(CursorWindow.java:108)
    at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:138)
    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132)
    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:220)
    at android.database.AbstractCursor.moveToNext(AbstractCursor.java:269)
    at com.example.cattleapp.helpers.DatabaseHelper.getGeneratedData(DatabaseHelper.java:217)
    at com.example.cattleapp.activities.MainActivity.generate(MainActivity.java:536)
    at com.example.cattleapp.activities.MainActivity$7.afterTextChanged(MainActivity.java:241)

0 个答案:

没有答案