android - getting object from SQlite database? -
im trying data database , have checked there data in table.
im getting error:
process: com.example.olev.shoppinglist, pid: 4636 java.lang.outofmemoryerror: failed allocate 13571692 byte allocation 3197016 free bytes , 3mb until oom @ java.util.arraylist.add(arraylist.java:118) @ com.example.olev.shoppinglist.dbhandler.getproductsfromdb(dbhandler.java:63) @ com.example.olev.shoppinglist.mainactivity.getproductsfromdb(mainactivity.java:66) @ com.example.olev.shoppinglist.mainactivity.oncreate(mainactivity.java:39) @ android.app.activity.performcreate(activity.java:5933) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) @ android.app.activitythread.performlaunchactivity(activitythread.java:2251) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1278) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
my dbhandler:
public void oncreate(sqlitedatabase db) { string query= "create table " + table_products + " ("+ column_id + " integer primary key autoincrement," + column_productname + " text," + column_ischecked + " boolean" + ");"; db.execsql(query); } public arraylist<product> getproductsfromdb(){ arraylist<product> products= new arraylist<>(); sqlitedatabase db=getwritabledatabase(); string query= "select * "+ table_products +";"; cursor c= db.rawquery(query,null); c.movetofirst(); while (!c.isafterlast()){ if(c.getstring(c.getcolumnindex("productname"))!=null){ product product=new product(integer.parseint(c.getstring(c.getcolumnindex(column_id))),column_productname,boolean.valueof(c.getstring(c.getcolumnindex(column_ischecked)))); products.add(product); } } db.close(); return products; }
and main:
arrayadapter<product> adapter; arraylist<product> productnames=new arraylist<>(); dbhandler dbhandler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview productlist=(listview)findviewbyid(android.r.id.list); dbhandler=new dbhandler(this,null,null,1); getproductsfromdb(); adapter= new customadapter(this,productnames); productlist.setadapter(adapter); } public void getproductsfromdb() { arraylist<product> products=dbhandler.getproductsfromdb(); productnames.addall(products); adapter.notifydatasetchanged(); }
so sum getproductsfromdb() somehow producing error.
you forgot c.movetonext();
. means read first row database again , again, , create same product
again , again, , add list again , again forever, , out of memory error. this:
[...] while (!c.isafterlast()){ if(c.getstring(c.getcolumnindex("productname"))!=null) { product product=new product(integer.parseint(c.getstring(c.getcolumnindex(column_id))),column_productname,boolean.valueof(c.getstring(c.getcolumnindex(column_ischecked)))); products.add(product); } c.movetonext(); }
Comments
Post a Comment