In my App, I have a form. In which user puts CNIC first. If his CNIC exists already in SQLite database. User's name and mobile number will automatically be filled in the edittexts (using TextWatcher). But whenever I type a same CNIC, app crashes. If CNIC exists in database, instead of auto filling the name and phone field, the app crashes. I've tried very much but couldn't solve my problem.
Query Method:
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;
}
Query Method called :
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 code :
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)