here basic function of fragment: 1. retrieves data backend service called backendless.com (a name , phone number) in oncreate method. 2. stores data in local sqlite database, used populate listview. user wants while in fragment(adds new contacts, deletes old ones , on) 3. saves manipulated data sqlite database backend service user exits fragment. happens within onpause method. works fine until onpause() method reached , java.lang.outofmemory
error
here sqlite data manger class, stores name , phone number locally.
public class dbadapter { private final static string tag = "dbadapter"; //field names public final static string key_rowid = "_id"; public final static string key_name = "name"; public final static string key_number = "number"; public final static string[] all_keys = {key_rowid,key_name,key_number}; //database info public static final string database_name = "contactsdatabase"; public static final string database_table = "contacts"; public static final int database_version = 1; //sql create database private static final string database_create_sql = "create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " + key_name + " text, " + key_number + " text" +");"; private final context context; private databasehelper mydbhelper; private sqlitedatabase db; public dbadapter(context c){ this.context = c; mydbhelper = new databasehelper(context); } public dbadapter open(){ db = mydbhelper.getwritabledatabase(); return this; } public void close(){ db.close(); } public long insertrow(string name,string number){ contentvalues values = new contentvalues(); values.put(key_name, name); values.put(key_number, number); return db.insert(database_table,null,values); } public boolean deleterow(long rowid){ string = key_rowid + "=" + rowid; return db.delete(database_table,where,null) !=0; } public void deleteall(){ cursor c = getallrows(); long rowid = c.getcolumnindexorthrow(key_rowid); if (c.movetofirst()){ do{ deleterow(c.getlong((int)rowid)); }while(c.movetonext()); } } public cursor getallrows(){ string = null; cursor c = db.query(database_table, all_keys, null, null, null, null, key_name + " asc"); if (c!=null){ c.movetofirst(); } return c; } // specific row rowid public cursor getrow(long rowid){ string = key_rowid + "=" + rowid; cursor c = db.query(true, database_table, all_keys, where, null, null, null, null,null); if (c!=null){ c.movetofirst(); } return c; } //change existing row new data public boolean updaterow(long rowid, string name, string num){ string = key_rowid + "=" +rowid; contentvalues values = new contentvalues(); values.put(key_name,name); values.put(key_number,num); return db.update(database_table,values,where,null)!=0; } private static class databasehelper extends sqliteopenhelper { public databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(database_create_sql); log.v("adapter", "updated"); } @override public void ondowngrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + database_table); db.execsql(database_create_sql); log.v("adapter", "downgraded"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + database_table); oncreate(db); } }}
and here fragment code. app keeps crashing when i'm trying retrieve data local database on onpause() method:
public class contactsfragments extends fragment { public dbadapter mydb; edittext inputname,inputnumber; private view view; private button savebutton, deleteallbutton; public listview mylist; public long rowclickedforedit = -1; public boolean shouldweedit = false; private arraylist<string> numbersforweb = new arraylist<>(); private arraylist<string> namesforweb = new arraylist<>(); public contactsfragments() {setretaininstance(true);} @override public void oncreate(bundle savedinstancestate) { final string useremail = backendless.userservice.currentuser().getemail(); toast.maketext(getactivity(), "ue: " + useremail, toast.length_long).show(); retrievedatafromcloud(useremail); super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view = inflater.inflate(r.layout.fragment_contacts_fragments, container, false); inputname = (edittext)view.findviewbyid(r.id.inputname); inputnumber = (edittext)view.findviewbyid(r.id.inputnumber); savebutton = (button)view.findviewbyid(r.id.savebutton); deleteallbutton = (button)view.findviewbyid(r.id.deleteallbutton); opendb(); populatelistview(); listviewitemcancel(); savebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (shouldweedit) { mydb.updaterow(rowclickedforedit, inputname.gettext().tostring(), inputnumber.gettext().tostring() ); populatelistview(); } else { onclick_add(v); } shouldweedit = false; inputname.settext(""); inputnumber.settext(""); inputname.sethint("enter contact name"); inputnumber.sethint("enter contact number"); final inputmethodmanager imm = (inputmethodmanager) getactivity().getsystemservice(context.input_method_service); imm.hidesoftinputfromwindow(getview().getwindowtoken(), 0); } }); deleteallbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onclickdeleteall(v); shouldweedit = false; inputname.settext(""); inputnumber.settext(""); inputname.sethint("enter contact name"); inputnumber.sethint("enter contact number"); } }); mylist = (listview)view.findviewbyid(r.id.listviewfragment); return view; } private void opendb(){ mydb = new dbadapter(getactivity()); mydb.open(); } public void onclick_add(view v){ if (!textutils.isempty(inputname.gettext().tostring())){ mydb.insertrow(inputname.gettext().tostring(), inputnumber.gettext().tostring()); }else{ toast.maketext(getactivity(), "please enter name", toast.length_long).show(); } populatelistview(); } public void populatelistview(){ cursor cursor = mydb.getallrows(); string[] fromfieldnames = new string[] {dbadapter.key_name,dbadapter.key_number}; int[] toviewids = new int[] {r.id.customrowcontactname,r.id.customrowcontactnumber}; cursoradapter cursoradapter; cursoradapter = new cursoradapter(getactivity(),r.layout.contacts_custom_row,cursor,fromfieldnames,toviewids); mylist = (listview)view.findviewbyid(r.id.listviewfragment); mylist.setadapter(cursoradapter); } public void onclickdeleteall(view v){ mydb.deleteall(); populatelistview(); } private void listviewitemcancel(){ mylist = (listview) view.findviewbyid(r.id.listviewfragment); mylist.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { inputname.settext(""); inputnumber.settext(""); inputname.sethint("enter contact name"); inputnumber.sethint("enter contact number"); shouldweedit = false; populatelistview(); } }); } class cursoradapter extends simplecursoradapter { private cursor c; private context context; public cursoradapter(context context, int layout, cursor c, string[] from, int[] to) { super(context, layout, c, from, to); this.c = c; this.context = context; } @override public view getview(int pos, view inview, viewgroup parent) { view vix = inview; if (vix == null) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); vix = inflater.inflate(r.layout.contacts_custom_row, null); } this.c.movetoposition(pos); string name = this.c.getstring(this.c.getcolumnindex(dbadapter.key_name)); string num = this.c.getstring(this.c.getcolumnindex(dbadapter.key_number)); final string id = this.c.getstring(this.c.getcolumnindex(dbadapter.key_rowid)); textview textname = (textview) vix.findviewbyid(r.id.customrowcontactname); textname.settext(name); final textview textnum = (textview) vix.findviewbyid(r.id.customrowcontactnumber); textnum.settext(num); final textview textcancel = (textview)vix.findviewbyid(r.id.customrowcanceltext); textcancel.setvisibility(view.invisible); final textview deletetext = (textview)vix.findviewbyid(r.id.customrowdeletetext); final textview editinfo = (textview)vix.findviewbyid(r.id.customrowedittext); deletetext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mydb.deleterow(integer.valueof(id)); populatelistview(); } }); editinfo.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { textcancel.setvisibility(view.visible); inputname.settext(""); inputnumber.settext(""); inputname.sethint("enter new name"); inputnumber.sethint("enter new number"); rowclickedforedit = long.valueof(id); shouldweedit = true; } }); textnum.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { uri uri; intent intent = new intent(intent.action_dial); intent.setdata(uri.parse("tel:" + textnum.gettext().tostring())); startactivity(intent); } }); return vix; } } public void retrievedatafromcloud(string useremail){ string whereclause = "useremailid = '"+ useremail +"'"; backendlessdataquery dataquery = new backendlessdataquery(); dataquery.setwhereclause( whereclause ); backendless.persistence.of(localphonenum.class).find(dataquery, new asynccallback<backendlesscollection<localphonenum>>(){ @override public void handleresponse( backendlesscollection<localphonenum> foundcontacts ) { list<string> dataarraylist = new arraylist<string>(); (localphonenum temp : foundcontacts.getdata()){ dataarraylist.add(temp.getphonenum()); } if (dataarraylist.isempty()){ toast.maketext(getactivity(), "no data found", toast.length_short).show(); }else{ toast.maketext(getactivity(), dataarraylist.get(0), toast.length_long).show(); } } @override public void handlefault( backendlessfault fault ) { toast.maketext(getactivity(), "error" , toast.length_short).show(); } }); } @override public void onpause() { ** error occurs here cursor cursor = mydb.getallrows(); cursor.movetofirst(); while (!cursor.isafterlast()) { string data = cursor.getstring(cursor.getcolumnindex("name")); namesforweb.add(data); } string useremail = backendless.userservice.currentuser().getemail(); localphonenum localphonenum = new localphonenum(useremail, "617 8", "mark"); super.onpause(); backendless.persistence.save(localphonenum, new asynccallback<localphonenum>() { @override public void handleresponse(localphonenum localphonenum) { toast.maketext(getactivity().getapplicationcontext(), "saved cloud", toast.length_long).show(); } @override public void handlefault(backendlessfault backendlessfault) { toast.maketext(getactivity().getapplicationcontext(), "not saved cloud " + backendlessfault, toast.length_long).show(); } }); }}
does know why i'm getting error? app doing much? thank you!
Comments
Post a Comment