java - The content doesn't appears when I return to the previous activity -
i have android app, writing notes. when press edit button in second2 activity move second3 activity without problems (in second3 activity can edit note without problem). when press save button, return second2 nothing appears there! it's empty activity! me, please!
mainactivity class:
package com.twitter.i_droidi.mynotes; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.contextmenu; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; import android.widget.toast; import java.util.list; import com.google.android.gms.ads.adrequest; import com.google.android.gms.ads.adview; public class mainactivity extends actionbaractivity implements adapterview.onitemclicklistener { listview lv; notesdatasource nds; list<notesmodel> noteslist; string[] notes; int i; arrayadapter<string> adapter; adview madview; adrequest adrequest; public static long back_pressed; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); nds = new notesdatasource(this); lv = (listview) findviewbyid(r.id.lv); nds.open(); noteslist = nds.getallnotes(); nds.close(); notes = new string[noteslist.size()]; (i = 0; < noteslist.size(); i++) { notes[i] = noteslist.get(i).gettitle(); } adapter = new arrayadapter<string>(mainactivity.this, android.r.layout.simple_list_item_1, android.r.id.text1, notes); lv.setadapter(adapter); registerforcontextmenu(lv); lv.setonitemclicklistener(this); madview = (adview) findviewbyid(r.id.adview); adrequest = new adrequest.builder().build(); madview.loadad(adrequest); } @override public void onbackpressed() { if (back_pressed + 2000 > system.currenttimemillis()) super.onbackpressed(); else toast.maketext(this, "press once again exit!", toast.length_short).show(); back_pressed = system.currenttimemillis(); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent nview = new intent(this, second2.class); nview.putextra("id", noteslist.get(position).getid()); nview.addflags(intent.flag_activity_clear_top); startactivity(nview); } @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_delete, menu); super.oncreatecontextmenu(menu, v, menuinfo); } @override public boolean oncontextitemselected(menuitem item) { switch (item.getitemid()) { case r.id.delete: adapterview.adaptercontextmenuinfo info = (adapterview.adaptercontextmenuinfo) item.getmenuinfo(); nds.open(); nds.deletenote(noteslist.get(info.position).getid()); noteslist = nds.getallnotes(); nds.close(); notes = new string[noteslist.size()]; (i = 0; < noteslist.size(); i++) { notes[i] = noteslist.get(i).gettitle(); } adapter = new arrayadapter<string>(mainactivity.this, android.r.layout.simple_list_item_1, android.r.id.text1, notes); lv.setadapter(adapter); registerforcontextmenu(lv); lv.setonitemclicklistener(this); toast ndelete = toast.maketext(this, r.string.deleted, toast.length_long); ndelete.show(); } return super.oncontextitemselected(item); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.mainmenunewnote: intent nnote = new intent(this, second.class); startactivity(nnote); return true; case r.id.mainmenuabout: alertdialog.builder aboutdialog = new alertdialog.builder(this); aboutdialog.settitle(getstring(r.string.about_title)); aboutdialog.setmessage(r.string.about_body); aboutdialog.seticon(r.drawable.my_notes); aboutdialog.setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface aboutdialog, int witch) { // not anything. } }); aboutdialog.show(); return true; } return super.onoptionsitemselected(item); } }
second class:
package com.twitter.i_droidi.mynotes; import android.content.intent; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.widget.edittext; import android.widget.toast; public class second extends actionbaractivity { notesdatasource nds; edittext notetitle; edittext notebody; int id; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.second); intent in = getintent(); id = in.getintextra("id", 0); notetitle = (edittext) findviewbyid(r.id.note_title); notebody = (edittext) findviewbyid(r.id.note); nds = new notesdatasource(this); nds.open(); notesmodel note = nds.getnote(id); nds.close(); notetitle.settext(note.gettitle()); notebody.settext(note.getbody()); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_second, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.secondmenusave: if (!notetitle.gettext().tostring().isempty() && !notebody.gettext().tostring().isempty()) { nds.open(); nds.createnote(notetitle.gettext().tostring(), notebody.gettext().tostring()); nds.close(); toast nsave = toast.maketext(this, r.string.saved, toast.length_long); nsave.show(); intent nview = new intent(this, mainactivity.class); nview.addflags(intent.flag_activity_clear_top); startactivity(nview); } else { toast notsave = toast.maketext(this, r.string.do_not_save, toast.length_long); notsave.show(); } return true; } return super.onoptionsitemselected(item); } }
second2 class:
package com.twitter.i_droidi.mynotes; import android.content.intent; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.widget.textview; public class second2 extends actionbaractivity { textview viewnotetitle; textview viewnotebody; int id; notesdatasource nds; notesmodel note; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.second2); intent in = getintent(); id = in.getintextra("id", 0); viewnotetitle = (textview) findviewbyid(r.id.view_note_title); viewnotebody = (textview) findviewbyid(r.id.view_note); nds = new notesdatasource(this); nds.open(); note = nds.getnote(id); nds.close(); viewnotetitle.settext(note.gettitle()); viewnotebody.settext(note.getbody()); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_second2, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.second2menuedit: intent nedit = new intent(this, second3.class); nedit.putextra("id", note.getid()); nedit.addflags(intent.flag_activity_clear_top); startactivity(nedit); } return super.onoptionsitemselected(item); } }
second3 class:
package com.twitter.i_droidi.mynotes; import android.content.intent; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.widget.edittext; import android.widget.toast; public class second3 extends actionbaractivity { notesdatasource nds; edittext notetitle; edittext notebody; int id; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.second); intent in = getintent(); id = in.getintextra("id", 0); notetitle = (edittext) findviewbyid(r.id.note_title); notebody = (edittext) findviewbyid(r.id.note); nds = new notesdatasource(this); nds.open(); notesmodel note = nds.getnote(id); nds.close(); notetitle.settext(note.gettitle()); notebody.settext(note.getbody()); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_second, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.secondmenusave: if (!notetitle.gettext().tostring().isempty() && !notebody.gettext().tostring().isempty()) { nds.open(); nds.updatenote(id, notetitle.gettext().tostring(), notebody.gettext().tostring()); nds.close(); toast nsave = toast.maketext(this, r.string.saved, toast.length_long); nsave.show(); intent nview = new intent(this, second2.class); nview.addflags(intent.flag_activity_clear_top); startactivity(nview); } else { toast notsave = toast.maketext(this, r.string.do_not_save, toast.length_long); notsave.show(); } return true; } return super.onoptionsitemselected(item); } }
the second activity gets data intent these 2 lines:
intent in = getintent(); id = in.getintextra("id", 0);
so when go activity third activity when save note in following lines:
intent nview = new intent(this, second2.class); nview.addflags(intent.flag_activity_clear_top); startactivity(nview);
you have add id intent did in main activity class, might below, dont have notelist
array in third activity.
nview.putextra("id", noteslist.get(position).getid());
so easiest way data in activity call
finish();
this because second activity still in activity stack , dont have start again, can finish third activity , second show because last activity in stack. title wont updated you'll have start activity beginning code below:
when done saving note.
@override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.secondmenusave: if (!notetitle.gettext().tostring().isempty() && !notebody.gettext().tostring().isempty()) { nds.open(); nds.updatenote(id, notetitle.gettext().tostring(), notebody.gettext().tostring()); nds.close(); toast nsave = toast.maketext(this, r.string.saved, toast.length_long); nsave.show(); intent nview = new intent(this, second2.class); nview.addflags(intent.flag_activity_clear_top); nview.putextra("id", id); startactivity(nview); finish(); } else { toast notsave = toast.maketext(this, r.string.do_not_save, toast.length_long); notsave.show(); } return true; } return super.onoptionsitemselected(item); }
for last problem, in second activity override onbackpressed method , add code below:
@override public void onbackpressed() { intent intent = new intent(this, mainactivity.class); intent.addflags(intent.flag_activity_clear_top); startactivity(intent); finish(); }
Comments
Post a Comment