Fetching Data from Database Issue in Android -
i have 3 layout:
main layout(with button "profile")
profile layout(with textview show data database , 2 button named "edit" , "back main")
edit layout(with textview , edittext edit profile of user , 2 button again says "save" , "back profile")
methods related buttons: (button 1 star,related methods 2 stars)
*profile: **editm(view w)
*edit: **calledit(view w)
*back main: **backmain(view w)
*save:*savemethj(view w)
*back profile: **pbackmeth(view w)
public class mainactivity extends activity { textview t1; edittext e1,e2; databasehelper helper; //profile variables string ptrm; pdatabasehelper phelper; edittext ed1; edittext ed2; edittext ed3; edittext ed4; edittext ed5; edittext ed6; textview ptn; textview kt0; textview kt1; textview kt2; textview kt3;textview kt4;textview kt5;textview kt6; @override protected void oncreate(bundle savedinstancestate) { requestwindowfeature(window.feature_no_title); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); phelper=new pdatabasehelper(this); } public void backmain(view w){ setcontentview(r.layout.activity_main); } public void editm(view w){ setcontentview(r.layout.profilec); try{ pshow(); } catch(exception e){ toast.maketext(getbasecontext(), "pshow method", 5).show(); } } public void pshow() { onstartp(); log.d("7", "a"); onstart(); log.d("10", "a"); try{ log.d("55555", "entering db"); cursor c=phelper.pshowrecord(ptrm); log.d("11", "a"); if (c!=null){ log.d("c1", "a"); c.movetofirst(); log.d("cmm1", "a"); string name= c.getstring(0); log.d("cn1", "a"); kt1.settext(name); log.d("c2", "a"); kt2.settext(c.getstring(1)); log.d("c3", "a"); kt3.settext(c.getstring(2)); log.d("12", "a"); kt4.settext(c.getstring(3)); kt5.settext(c.getstring(4)); kt6.settext(c.getstring(5)); log.d("13", "a"); } else{ log.d("14", "a"); toast.maketext(this,"add contact", 50).show(); } }catch(exception e){ toast.maketext(getbasecontext(), "some display error", 20).show(); } } public void pbackmeth(view w){ setcontentview(r.layout.profilec); pshow(); } public void calledit(view w) { setcontentview(r.layout.editc); } public void onstartp(){ kt1=(textview)findviewbyid(r.id.ptext3); kt2=(textview)findviewbyid(r.id.ptext5); kt3=(textview)findviewbyid(r.id.ptext7); kt4=(textview)findviewbyid(r.id.ptext9); kt5=(textview)findviewbyid(r.id.ptext11); kt6=(textview)findviewbyid(r.id.ptext13); } public void onstart(){ ed1=(edittext)findviewbyid(r.id.jname); ed2=(edittext)findviewbyid(r.id.jedittext1); ed3=(edittext)findviewbyid(r.id.jedittext2); ed4=(edittext)findviewbyid(r.id.jedittext3); ed5=(edittext)findviewbyid(r.id.jedittext4); ed6=(edittext)findviewbyid(r.id.jaedittext1); ptn=(textview)findviewbyid(r.id.jprofile); } public void savemethj(view w) { onstart(); log.d("tag000", "before allgettext"); long a; log.d("tag01111", "afterlong"); ptrm=ptn.gettext().tostring(); try{ phelper.deleterecord(ptrm); log.d("tag02222", "be"); string edt1=ed1.gettext().tostring(); string edt2=ed2.gettext().tostring(); log.d("tag03333", "be11111"); string edt3=ed3.gettext().tostring(); string edt4=ed4.gettext().tostring(); log.d("tag0444444", "be2222"); string edt5=ed5.gettext().tostring(); string edt6=ed6.gettext().tostring(); log.d("tag11111", "after allgettext"); phelper.deleterecord(ptrm); a=phelper.insertpro(ptrm,/*img*/edt1, edt2, edt3, edt4, edt5, edt6); log.d("tag9999999", "after allgettext"); if(a>=1){ toast.maketext(getbasecontext(),a+ "record saved", 30).show(); } else{ toast.maketext(getbasecontext(), "not saved", 30).show(); }} catch(exception e) { toast.maketext(getbasecontext(), "errrrrrrrrrrr", toast.length_short).show(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } }
database class :
public class pdatabasehelper extends sqliteopenhelper { final static string databasename="demonewexp"; final static int databaseversion=1; public pdatabasehelper(context ctx){ super(ctx,databasename,null,databaseversion); } @override public void oncreate(sqlitedatabase db) { try{ log.d("tag4545","database"); db.execsql("create table mypro(id text,name text,guardian text,number text,address text,medical text,another text)"); } catch(sqlexception e){e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists mypro"); oncreate(db); } public long insertpro(string id,string e1,string e2,string e3,string e4,string e5,string e6) { sqlitedatabase base=getwritabledatabase(); contentvalues value=new contentvalues(); value.put("id", id); value.put("name", e1); value.put("guardian", e2); value.put("number", e3); value.put("address", e4); value.put("medical", e5); value.put("another", e6); long a=base.insert("mypro",null,value); return a; } public cursor pshowrecord(string id) { log.d("d1", "a"); cursor cs = null; sqlitedatabase base=getreadabledatabase(); log.d("d2", "a"); try { log.d("d3", "a"); cs=base.query("mypro", new string[]{"name","guardian","number","address","medical","another"},"id=?", new string[]{string.valueof(id)}, null, null, null); log.d("d4", "a"); return cs; }catch(exception e){ log.d("d5", "a"); return cs; } } public void deleterecord(string pe_id) { sqlitedatabase base=getwritabledatabase(); base.delete("mypro","id=?",new string[]{pe_id}); } }
i start application 1st time,i clicked "profile" button,there no data therefore shows toast "some display error" click edit button , add data name,number etc click save button,it saved. move profile layout clicking "back profile" , can see added profile or data. if move main layout clicking "back main" , if again click "profile" button take me profile layout added data fetched everytime database. here come problem when close application , start again click "profile" button,then not showing database values there in database when start app again unable fetch data have stored , showing toast "some display error". thought check out errors , put "log" see log file when working application showing last value as:
log.d("cmm1", "a");
after application stopped , no logs generated in log file. means error while working with:
string name= c.getstring(0);
i dont know problem code or may have other problem. please me out. missing something?
k have edited code intent start new activity. have other code cut paste code make easy view , find error. here code now:
(1st activity button profile,clicking profile button take activity intent)
mainactivity.class:
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { requestwindowfeature(window.feature_no_title); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main);phelper=new pdatabasehelper(this); } public void editm(view w){ intent pro=new intent(mainactivity.this,profile.class); startactivity(new intent(pro)); } public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } }
(to show profile)
profile.class:
public class profile extends activity { pdatabasehelper phelper; textview ptn; textview kt0; textview kt1; textview kt2; textview kt3;textview kt4;textview kt5;textview kt6; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); toast.maketext(getbasecontext(), "profile view", 5).show(); setcontentview(r.layout.profilec); log.d("oncreate5", "5"); ptn=(textview)findviewbyid(r.id.jprofile); log.d("oncreate5.666", "5.6"); phelper=new pdatabasehelper(this); log.d("oncreate5", "6"); pshow(); log.d("oncreate5", "7"); toast.maketext(getbasecontext(), "helloo new", 10).show(); } public void pshow(){ log.d("6", "a"); //toast.maketext(getbasecontext(), "helloo new", 10).show(); onstartp(); log.d("7", "a"); cursor c=null; log.d("8", "a"); log.d("9,10", "a"); try{ log.d("55555", "entering db"); string ptrm = null; c=phelper.pshowrecord(ptrm); log.d("11", "a"); if(c!=null){ log.d("c1", "a"); c.movetofirst(); log.d("cmm1", "a"); string name= c.getstring(0); log.d("cn1", "a"); kt1.settext(name); log.d("c2", "a"); kt2.settext(c.getstring(1)); log.d("c3", "a"); kt3.settext(c.getstring(2)); log.d("12", "a"); kt4.settext(c.getstring(3)); kt5.settext(c.getstring(4)); kt6.settext(c.getstring(5)); log.d("13", "a"); c.close(); log.d("14", "a"); } else{ toast.maketext(this,"add contact", 50).show(); } }catch(exception e){ toast.maketext(getbasecontext(), "some display error", 20).show(); } } public void calledit(view w) { intent i=new intent(this,editp.class); startactivity(i); //setcontentview(r.layout.editc); } public void onstartp(){ kt1=(textview)findviewbyid(r.id.ptext3); kt2=(textview)findviewbyid(r.id.ptext5); kt3=(textview)findviewbyid(r.id.ptext7); kt4=(textview)findviewbyid(r.id.ptext9); kt5=(textview)findviewbyid(r.id.ptext11); kt6=(textview)findviewbyid(r.id.ptext13); } }
(to add or edit profile save button) editp.class
public class editp extends activity { edittext ed1; edittext ed2; edittext ed3; edittext ed4; edittext ed5; edittext ed6; textview ptn; pdatabasehelper phelper; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.editc); phelper=new pdatabasehelper(this); } public void onstart(){ ed1=(edittext)findviewbyid(r.id.jname); ed2=(edittext)findviewbyid(r.id.jedittext1); ed3=(edittext)findviewbyid(r.id.jedittext2); ed4=(edittext)findviewbyid(r.id.jedittext3); ed5=(edittext)findviewbyid(r.id.jedittext4); ed6=(edittext)findviewbyid(r.id.jaedittext1); ptn=(textview)findviewbyid(r.id.jprofile); } public void savemethj(view w) { onstart(); log.d("tag000", "before allgettext"); long a; log.d("tag01111", "afterlong"); string ptrm = ptn.gettext().tostring(); try{ phelper.deleterecord(ptrm); log.d("tag02222", "be"); string edt1=ed1.gettext().tostring(); string edt2=ed2.gettext().tostring(); log.d("tag03333", "be11111"); string edt3=ed3.gettext().tostring(); string edt4=ed4.gettext().tostring(); log.d("tag0444444", "be2222"); string edt5=ed5.gettext().tostring(); string edt6=ed6.gettext().tostring(); log.d("tag11111", "after allgettext"); phelper.deleterecord(ptrm); a=phelper.insertpro(ptrm,edt1, edt2, edt3, edt4, edt5, edt6); if(a>=1){ toast.maketext(getbasecontext(),a+ "record saved", 30).show(); } else{ toast.maketext(getbasecontext(), "not saved", 30).show(); }} catch(exception e) { toast.maketext(getbasecontext(), "errrrrrrrrrrr", toast.length_short).show(); } } }
my records saved in database can see them,but unable @ records in profile have saved first time called pshow() fetch out record database. want everytime come profile can see records either moving edit page or clicking "profile" button on main page or layout. please me find error. , excuse log commands have put see errors. , getting error here:
string name= c.getstring(0);
because last log see while running program in log file is:
log.d("cmm1", "a");
after error message displayed on screen toast "some display error".
you have lot of work consider this.
instead of:
public void editm(view w){ setcontentview(r.layout.profilec); try{ pshow(); } catch(exception e){ toast.maketext(getbasecontext(), "pshow method", 5).show(); } }
you want:
intent edit_profile_intent = new intent(); edit_profile_intent.setclass(getapplicationcontext(), editprofileactivity.class); startactivity(edit_profile_intent);
so need several things: create new activity class, declare in manifest, edit layout include appropriate views, etc.
lets fix activity stack , worry data problems...
Comments
Post a Comment