android - Need help with onClick and intents -


i'm working , app based on notepad tutorial...in tutorial click on note in listview , taken edit corresponding information shown, make changes select confirm..and taken main.

i added second activity app "contact" when click on note in listview , taken contact corresponding information shown including button............ click button , taken edit.........and problem pops ugly head. when ckick button blank screen , forced close popup........

here code main taking contact....this works information shown

protected void onlistitemclick(listview l, view v, int position, long id) {     super.onlistitemclick(l, v, position, id);     intent = new intent(this, contact.class);     i.putextra(notesdbadapter.key_rowid, id);     startactivityforresult(i, activity_edit); } 

here code takes contact edit.....this gives me error, i'm sure has "intent.putextra(notesdbadapter.key_rowid, id);" needing recoded work onclick compared onlistitemclick........i'm thinking id not being passed over

public void onclick(view v) { string id = null; switch (v.getid()) { case r.id.admin: // dostuff     intent intent = new intent(this, contact.class);     intent.putextra(notesdbadapter.key_rowid, id);     startactivity(intent);      break; 

could show me how button????

here code main, contacts , edit..........if need .......thanks

main.class

public class main extends listactivity { private static final int activity_create=0; private static final int activity_edit=1;  private static final int insert_id = menu.first; private static final int delete_id = menu.first + 1; private static final int = menu.first + 2; private static final int testbutton = menu.first + 3; private static final int contact_id = menu.first + 4; private static final int cancel_id = menu.first + 5;  private notesdbadapter mdbhelper;  /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);     mdbhelper = new notesdbadapter(this);     mdbhelper.open();     filldata();     registerforcontextmenu(getlistview()); }  private void filldata() {     cursor notescursor = mdbhelper.fetchallnotes();     startmanagingcursor(notescursor);      // create array specify fields want display in list (only title)     string[] = new string[]{notesdbadapter.key_name, notesdbadapter.key_rank};       // , array of fields want bind fields (in case text1)     int[] = new int[]{r.id.text1, r.id.text2};      // create simple cursor adapter , set display     simplecursoradapter notes =          new simplecursoradapter(this, r.layout.mainlistrow, notescursor, from, to);     setlistadapter(notes); }  @override public boolean oncreateoptionsmenu(menu menu) {     super.oncreateoptionsmenu(menu);     menu.add(0, insert_id, 0, r.string.menu_insert);     menu.add(0, about, 0, r.string.menu_about);     menu.add(0, testbutton, 0, r.string.menu_test);     return true; }  @override public boolean onmenuitemselected(int featureid, menuitem item) {     switch(item.getitemid()) {         case insert_id:             createnote();             return true;         case about:                     menuabout();                     return true;         case testbutton:                     menutest();                     return true;         }      return super.onmenuitemselected(featureid, item); }  @override public void oncreatecontextmenu(contextmenu menu, view v,         contextmenuinfo menuinfo) {     super.oncreatecontextmenu(menu, v, menuinfo);     menu.add(0, contact_id, 0, r.string.menu_contact);     menu.add(0, delete_id, 0, r.string.menu_delete);     menu.add(0, cancel_id, 0, r.string.menu_cancel);  }  @override public boolean oncontextitemselected(menuitem item) {     switch(item.getitemid()) {         case delete_id:             adaptercontextmenuinfo info = (adaptercontextmenuinfo) item.getmenuinfo();             mdbhelper.deletenote(info.id);             filldata();             return true;         case contact_id:             contact();             return true;        }     return super.oncontextitemselected(item); }  private void menutest() {     intent = new intent(this, newmain.class);     startactivityforresult(i, activity_create);     log.i(this.tostring(), "menu test"); }  private void contact() {     intent = new intent(this, contact.class);     startactivityforresult(i, activity_create);     log.i(this.tostring(), "menu contact"); }      private void createnote() {     intent = new intent(this, admin.class);     startactivityforresult(i, activity_create); }  public void menuabout() {     final dialog dialog = new dialog(main.this);     dialog.setcontentview(r.layout.about);     dialog.settitle("about nco leaders book");           dialog.setcancelable(true);     button button = (button) dialog.findviewbyid(r.id.aboutbutton);     button.setonclicklistener(new onclicklistener() {      @override     public void onclick(view v) {         dialog.dismiss();     }               });               //now dialog set up, it's time show                   dialog.show(); }   @override protected void onlistitemclick(listview l, view v, int position, long id) {     super.onlistitemclick(l, v, position, id);     intent = new intent(this, contact.class);     i.putextra(notesdbadapter.key_rowid, id);     startactivityforresult(i, activity_edit); }  @override protected void onactivityresult(int requestcode, int resultcode, intent intent) {     super.onactivityresult(requestcode, resultcode, intent);     filldata(); } 

}

contact.class

    public class contact extends activity implements onclicklistener{         private edittext mnametext;     private edittext mranktext;     private long mrowid;     private notesdbadapter mdbhelper;     protected cursor cursor;     /** called when activity first created. */     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         mdbhelper = new notesdbadapter(this);         mdbhelper.open();         setcontentview(r.layout.contact);          button adminbutton = (button) this.findviewbyid(r.id.admin);         adminbutton.setonclicklistener(this);           mnametext = (edittext) findviewbyid(r.id.name);         mranktext = (edittext) findviewbyid(r.id.rank);          mrowid = (savedinstancestate == null) ? null :             (long) savedinstancestate.getserializable(notesdbadapter.key_rowid);         if (mrowid == null) {             bundle extras = getintent().getextras();             mrowid = extras != null ? extras.getlong(notesdbadapter.key_rowid)                                     : null;}            populatefields();     };   private void populatefields() {     if (mrowid != null) {         cursor note = mdbhelper.fetchsoldiers(mrowid);         startmanagingcursor(note);         mnametext.settext(note.getstring(                 note.getcolumnindexorthrow(notesdbadapter.key_name)));         mranktext.settext(note.getstring(                 note.getcolumnindexorthrow(notesdbadapter.key_rank)));     } }  //@override public void onclick(view v) {     string id = null;     switch (v.getid()) {     case r.id.admin: // dostuff         intent intent = new intent(this, edit.class);         intent.putextra(notesdbadapter.key_rowid, id);         startactivity(intent);         log.i("onclick", "endingtestnext");             break; }}  } 

edit.class

public class edit extends activity {  private edittext mnametext; private edittext mranktext; private edittext mssntext; private long mrowid; private notesdbadapter mdbhelper;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     mdbhelper = new notesdbadapter(this);     mdbhelper.open();      setcontentview(r.layout.edit);     settitle(r.string.edit_note);      mnametext = (edittext) findviewbyid(r.id.name);     mranktext = (edittext) findviewbyid(r.id.rank);     mssntext = (edittext) findviewbyid(r.id.ssn);      button confirmbutton = (button) findviewbyid(r.id.confirm);       mrowid = (savedinstancestate == null) ? null :         (long) savedinstancestate.getserializable(notesdbadapter.key_rowid);     if (mrowid == null) {         bundle extras = getintent().getextras();         mrowid = extras != null ? extras.getlong(notesdbadapter.key_rowid)                                 : null;     }      populatefields();      confirmbutton.setonclicklistener(new view.onclicklistener() {          public void onclick(view view) {             setresult(result_ok);             finish();         }      }); }  private void populatefields() {     if (mrowid != null) {         cursor note = mdbhelper.fetchnote(mrowid);         startmanagingcursor(note);         mnametext.settext(note.getstring(                 note.getcolumnindexorthrow(notesdbadapter.key_name)));         mranktext.settext(note.getstring(                 note.getcolumnindexorthrow(notesdbadapter.key_rank)));         mssntext.settext(note.getstring(                 note.getcolumnindexorthrow(notesdbadapter.key_ssn)));     } }  @override protected void onsaveinstancestate(bundle outstate) {     super.onsaveinstancestate(outstate);     savestate();     outstate.putserializable(notesdbadapter.key_rowid, mrowid); }  @override protected void onpause() {     super.onpause();     savestate(); }  @override protected void onresume() {     super.onresume();     populatefields(); }  private void savestate() {     string name = mnametext.gettext().tostring();     string rank = mranktext.gettext().tostring();     string ssn = mssntext.gettext().tostring();      if (mrowid == null) {         long id = mdbhelper.createnote(name, rank, ssn);         if (id > 0) {             mrowid = id;         }     } else {         mdbhelper.updatenote(mrowid, name, rank, ssn);     } }} 

here's think going on. contact class expects intent starts should have long under key_rowid key. in onclick method, setting string null value. when contact.oncreate tries retrieve it, getting explicit null string value, can't assigned long mrowid variable. don't call putextra, since don't have data pass.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -