Java.lang.outofmemoryerror: [memory Exhausted] While Reading Data From Sqlite Android
I have successfully saved my data in SQLite DB. But I am getting an error while reading data from my SQLite DB and then my app is crashing. error message 01-01 05:42:13.916 607-6
Solution 1:
Your loop never ends because in each iteration you set the cursor's index at the 1st row with moveToFirst()
without advancing to the next row.
Use moveToNext()
only:
publicvoidReadSqliteData(Context context){
ArrayList<Model> list = newArrayList<>();
Adpteradpter=newAdpter(list,context);
SQLiteDatabasedatabase= getWritableDatabase();
Cursorcursor= database.rawQuery("Select name, image from orders",null);
while (cursor.moveToNext()){
Modelmodel=newModel();
model.setImage(cursor.getString(0));
model.setName(cursor.getString(1));
list.add(model);
}
adpter.notifyDataSetChanged();
cursor.close();
database.close();
}
Solution 2:
Try with the following code.
//update your tablecreate query, id should be auto increment and also your query was notin correct form.
db.execSQL(
"Create Table orders"+
"("+ id INTEGERPRIMARY KEY AUTOINCREMENT,"+
"image text ,"+
"name text" +")"
);
//update you read method code
public void ReadSqliteData(Context context){
ArrayList<Model> list = new ArrayList<>();
Adpter adpter = new Adpter(list,context);
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.rawQuery("Select name, image from orders",null);
if (cursor.moveToNext()){
while (cursor.moveToFirst()){
Model model = new Model();
model.setImage(cursor.getString(0));
model.setName(cursor.getString(1));
list.add(model);
}
adpter.notifyDataSetChanged();
}
cursor.close();
database.close();
}
Post a Comment for "Java.lang.outofmemoryerror: [memory Exhausted] While Reading Data From Sqlite Android"