java - Android Studio: SQlite db ListView items disappear on startActivity -
i new android studio , app creation. followed johhny mansons youtube quide new apps, , extended work greatly.
my problem is, logging out of app removes items list view, upon new login.
when implemented login functionality, changed loginactivity main page, following startactivity reach mainactivity.java app functional once log in. can submit picture , text, saved sqlite , display in listview tab.
however, whenever log out, or tab back, , login again. data wiped. believe because startactivity executes mainactivity anew, creating new database , removing old. not quite sure of cause , why happens. before implmeneting loginactivity, app retain data fine.
loginactivity (login page)
mlogin = (button) findviewbyid(r.id.obutton); mlogin.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { user user = new user(dbhandlerusers.getusercount(), string.valueof(musername.gettext()), string.valueof(mpassword.gettext()), null); if (validuser(user)) { currentuser g = currentuser.getinstance(); g.setcurrentuser(string.valueof(musername.gettext())); intent intent = new intent(loginactivity.this, mainactivity.class); startactivity(intent); return; } toast.maketext(getapplicationcontext(), "wrong username or password", toast.length_short).show(); } });
mainactivity (function handling questions)
it populatelist() should mylistview display questions. might worth noting using 2 databases, 1 users , 1 questions.
public class mainactivity extends actionbaractivity { private static final int edit = 0, delete = 1; edittext questiontxt; //include picture imageview questionimageimgview; list<question> questions = new arraylist<question>(); list<question> otherquestions = new arraylist<question>(); //preparing incoming data listview mylistview; listview otherlistview; uri imageuri = uri.parse("android.resource://kaist624.projekt.kse624_projekt1"+r.drawable.no_user_logo); databasehandler dbhandler; double mylongitude = 0.0d, mylatitude = 0.0d; //later remove 0.0d double qlongitude = 0.0d, qlatitude = 0.0d; int longclickeditemindex; arrayadapter<question> questionadapter; private float currentvalue; private long lastupdate; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); dbhandler = new databasehandler(getapplicationcontext()); questiontxt = (edittext) findviewbyid(r.id.txtquestion); mylistview = (listview) findviewbyid(r.id.listview); otherlistview = (listview) findviewbyid(r.id.listview2); questionimageimgview = (imageview) findviewbyid(r.id.imgquestion); //this make our items in listview clickable on event. registerforcontextmenu(mylistview); mylistview.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long id) { longclickeditemindex = position; //position tell item clicked return false; } }); tabhost tabhost = (tabhost) findviewbyid(r.id.tabhost); tabhost.setup(); ... final button addbtn = (button) findviewbyid(r.id.btnadd); addbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //find logged in user currentuser user = currentuser.getinstance(); string currentuser=user.getcurrentuser(); //add question question question = new question(dbhandler.getquestionscount(), string.valueof(questiontxt.gettext()), qlongitude, qlatitude, imageuri, currentuser); dbhandler.createquestion(question); questions.add(question); questionadapter.notifydatasetchanged(); toast.maketext(getapplicationcontext(), string.valueof(questiontxt.gettext()) + " has been added", toast.length_short).show(); clearfields(); } }); questiontxt.addtextchangedlistener(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) { addbtn.setenabled(string.valueof(questiontxt.gettext()).trim().length() > 0); } @override public void aftertextchanged(editable s) { } }); //populate list view questions if (dbhandler.getquestionscount() !=0) //if there contacts questions.addall(dbhandler.getallquestions()); //add content populatelist(); } ... dhandler.deletequestion(questions.get(longclickeditemindex)); questions.remove(longclickeditemindex); questionadapter.notifydatasetchanged(); break; } return super.oncontextitemselected(item); } public void onactivityresult(int reqcode, int rescode, intent data) { if (rescode == result_ok) { if (reqcode == 1) { imageuri = data.getdata(); questionimageimgview.setimageuri(data.getdata()); } } } private void populatelist() { questionadapter = new questionlistadapter(); mylistview.setadapter(questionadapter); //add method search other phone's adapter list otherlistview.setadapter(questionadapter); } private class questionlistadapter extends arrayadapter<question> { //this creates group of question, distance , image - 1 object public questionlistadapter() { super (mainactivity.this, r.layout.listview_item, questions); } @override public view getview(int position, view view, viewgroup parent) { if (view == null) view = getlayoutinflater().inflate(r.layout.listview_item, parent, false); question currentquestion = questions.get(position); textview question = (textview) view.findviewbyid(r.id.qquestion); question.settext(currentquestion.getquestion()); textview username = (textview) view.findviewbyid(r.id.qlongitude); username.settext(currentquestion.getuser()); //textview longitude = (textview) view.findviewbyid(r.id.qlongitude); //longitude.settext(currentquestion.getuser()); textview latitude = (textview) view.findviewbyid(r.id.qlatitude); latitude.settext(double.tostring(currentquestion.getlatitude()) + " meters"); imageview questionimage = (imageview) view.findviewbyid(r.id.qimageview); questionimage.setimageuri(currentquestion.getimageuri()); //textview distance = (textview) view.findviewbyid(r.id.qdistance); //distance.settext(double.tostring(currentquestion.getdistance())); return view; } }
let me know if should provide more information.
class overview - currentuser - databasehandler - databasehandleruser - loginactivity
- mainactivity
- question
- registeractivity - user
looking @ code hard find issue. can make out is:
- try check if have created database correctly.
- if database correct try debug if getting data database in list view object.
- if getting data may not setting data in listview.
study lifecycle of activity can understand how view behaving under circumstances.
let me know if face issue in debugging code.
Comments
Post a Comment