java - Displaying data in main Activity from two other Activities -


i'm new android studio. trying data editmessage , editsendto activities testexplicitintents activity. school project, required create editsendto activity , display phone number in testexplicitintents activity.

i can following lessons on other activities, learn how display data both edit activities. have tried several approaches try ends either editmessage or editsendto producing null results once done button pressed.

public class testexplicitintents extends activity {      public static final string class_tag = "testexplicitintents";     public static final int new_message_request = 1;     public static final int new_phone_request = 1;      private string message = "";     private string phone = "";      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_test_explicit_intents);          // getting views defined in xml files.         textview tvmessagedetails = (textview) findviewbyid(r.id.tvmessagedetails);         tvmessagedetails.setbackgroundcolor(color.green);         tvmessagedetails.setmovementmethod(new scrollingmovementmethod());         message ="is st. patricks day?";         phone = "";         setsummary();          // responding event - onclick edit message button         // using named inner class         button btneditmessage;         btneditmessage = (button) this.findviewbyid(r.id.btneditmessage);         handlebuttoneditmessageonclick buttoneditmessageonclick;         buttoneditmessageonclick = new handlebuttoneditmessageonclick();         btneditmessage.setonclicklistener(buttoneditmessageonclick);          // responding event - onclick edit send button         // using named inner class         button btneditsendto;         btneditsendto = (button) this.findviewbyid(r.id.btneditsendto);         handlebuttoneditsendtoonclick buttoneditsendtoonclick;         buttoneditsendtoonclick = new handlebuttoneditsendtoonclick();         btneditsendto.setonclicklistener(buttoneditsendtoonclick);      }      /**      * put summary of phone , message , display it.      */     private void setsummary() {         stringbuilder summary;          summary = new stringbuilder("sending to:\n");         summary.append(phone);         summary.append("\n\nmessage:\n");         summary.append(message);         textview tvmessagedetails = (textview) findviewbyid(r.id.tvmessagedetails);         tvmessagedetails.settext(summary);     }     /**      * handle edit button onclick starting activity example of      * starting activity using explicit intent.      *      */     @suppresswarnings("rawtypes")     public class handlebuttoneditmessageonclick implements onclicklistener {          public static final string class_tag = "handlebuttoneditmessageonclick";          public void onclick(view v) {             log.i(class_tag, "onclick started...");              // example of explicit intent, naming java class use             // (editmessage.class)             intent editintent;             activity sourceactivity;             class destinationclass;              sourceactivity = testexplicitintents.this;             destinationclass = editmessage.class;             editintent = new intent(sourceactivity, destinationclass);              // sending information intent receiver through intent object             editintent.putextra("current_message", testexplicitintents.this.message);              //startactivity(editintent);             startactivityforresult(editintent, new_message_request);         }     }      @suppresswarnings("rawtypes")     public class handlebuttoneditsendtoonclick implements onclicklistener {          public static final string class_tag = "handlebuttoneditsendtoonclick";          public void onclick(view v) {             log.i(class_tag, "onclick started...");              intent editsendintent;             activity startactivity;             class endclass;              startactivity = testexplicitintents.this;             endclass = editsendto.class;             editsendintent = new intent(startactivity, endclass);              // sending information intent receiver through intent object             editsendintent.putextra("current_phone", testexplicitintents.this.phone);              //startactivity(editintent);             startactivityforresult(editsendintent, new_phone_request);         }     }      @override       protected void onactivityresult(int requestcode, int resultcode, intent data){         super.onactivityresult(requestcode, resultcode, data);          string newmessage = getintent().getstringextra("new_message");         string curmessage = getintent().getstringextra("current_message");         string newphone = getintent().getstringextra("new_phone");         string curphone = getintent().getstringextra("current_phone");          // check request we're responding         if (requestcode == new_message_request) {             // make sure request successful             if (resultcode == result_ok) {                 message = newmessage;                 phone = curphone;                 setsummary();             }         }         if (requestcode == new_phone_request) {             if (resultcode == result_ok) {                 message = curmessage;                 phone = newphone;                 setsummary();             }         }     } } 

here editmessage:

public class editmessage extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_edit_message);         // intent activity. every activity has intent ,         // set edittext contents string in info comes         // intent         intent editintent;         edittext etmessage;         editintent = this.getintent();         string themessage;         themessage = editintent.getstringextra("current_message");         etmessage = (edittext) this.findviewbyid(r.id.etmessage);         etmessage.settext(themessage);          // event handler going done button can return         // new message         button btndone = (button) this.findviewbyid(r.id.btndone);         btndone.setonclicklistener(new buttondoneonclickhandler());     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_edit_message, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      /**      * handles button done onclick event creating resulting intent ,      * finishing      */     private class buttondoneonclickhandler implements onclicklistener {          public void onclick(view v) {              intent intent = new intent();             intent.putextra("new_message", ((edittext) findviewbyid(r.id.etmessage)).gettext().tostring());             setresult(result_ok, intent);             finish();         }     } } 

here editsendto:

public class editsendto extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_edit_send_to);          // intent activity. every activity has intent ,         // set edittext contents string in info comes         // intent         intent editintent;         edittext etphone;         editintent = this.getintent();         string thephone;         thephone = editintent.getstringextra("current_phone");         etphone = (edittext) this.findviewbyid(r.id.etphone);         etphone.settext(thephone);          // event handler going done button can return         // new message         button btndone = (button) this.findviewbyid(r.id.btndone);         btndone.setonclicklistener(new buttondoneonclickhandler());     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_edit_send_to, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      /**      * handles button done onclick event creating resulting intent ,      * finishing      */     private class buttondoneonclickhandler implements onclicklistener {          public void onclick(view v) {              intent intent = new intent();             intent.putextra("new_phone", ((edittext)  findviewbyid(r.id.etphone)).gettext().tostring());             setresult(result_ok, intent);             finish();         }     } } 

i tried figure out few days , have had no luck finding answer.
appreciate pointer in right direction.

your code should below :

testexplicitintents.java

public class testexplicitintents extends activity { public static final string class_tag = "testexplicitintents"; public static final int new_message_request = 1; public static final int new_phone_request = 2;  private string message = ""; private string phone = "";  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_test_explicit_intents);      // getting views defined in xml files.     textview tvmessagedetails = (textview) findviewbyid(r.id.tvmessagedetails);     tvmessagedetails.setbackgroundcolor(color.green);     tvmessagedetails.setmovementmethod(new scrollingmovementmethod());     message = "is st. patricks day?";     phone = "";     setsummary();      // responding event - onclick edit message button     // using named inner class     button btneditmessage;     btneditmessage = (button) this.findviewbyid(r.id.btneditmessage);     handlebuttoneditmessageonclick buttoneditmessageonclick;     buttoneditmessageonclick = new handlebuttoneditmessageonclick();     btneditmessage.setonclicklistener(buttoneditmessageonclick);      // responding event - onclick edit send button     // using named inner class     button btneditsendto;     btneditsendto = (button) this.findviewbyid(r.id.btneditsendto);     handlebuttoneditsendtoonclick buttoneditsendtoonclick;     buttoneditsendtoonclick = new handlebuttoneditsendtoonclick();     btneditsendto.setonclicklistener(buttoneditsendtoonclick);  }  /**  * put summary of phone , message , display it.  */ private void setsummary() {     stringbuilder summary;      summary = new stringbuilder("sending to:\n");     summary.append(phone);     summary.append("\n\nmessage:\n");     summary.append(message);     textview tvmessagedetails = (textview) findviewbyid(r.id.tvmessagedetails);     tvmessagedetails.settext(summary); }  /**  * handle edit button onclick starting activity example of  * starting activity using explicit intent.  */ @suppresswarnings("rawtypes") public class handlebuttoneditmessageonclick implements view.onclicklistener {      public static final string class_tag = "handlebuttoneditmessageonclick";      public void onclick(view v) {         log.i(class_tag, "onclick started...");          // example of explicit intent, naming java class use         // (editmessage.class)         intent editintent;         activity sourceactivity;         class destinationclass;          sourceactivity = testexplicitintents.this;         destinationclass = editmessage.class;         editintent = new intent(sourceactivity, destinationclass);          // sending information intent receiver through intent object         editintent.putextra("current_message", testexplicitintents.this.message);          //startactivity(editintent);         startactivityforresult(editintent, new_message_request);     } }  @suppresswarnings("rawtypes") public class handlebuttoneditsendtoonclick implements view.onclicklistener {      public static final string class_tag = "handlebuttoneditsendtoonclick";      public void onclick(view v) {         log.i(class_tag, "onclick started...");          intent editsendintent;         activity startactivity;         class endclass;          startactivity = testexplicitintents.this;         endclass = editsendto.class;         editsendintent = new intent(startactivity, endclass);          // sending information intent receiver through intent object         editsendintent.putextra("current_phone", testexplicitintents.this.phone);          //startactivity(editintent);         startactivityforresult(editsendintent, new_phone_request);     } }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);      // check request we're responding     if (requestcode == new_message_request) {         // make sure request successful         if (resultcode == result_ok) {             message = data.getstringextra("new_message");             setsummary();         }     } else if (requestcode == new_phone_request) {         if (resultcode == result_ok) {             phone = data.getstringextra("new_phone");             setsummary();         }     } } } 

editsendto class:

public class extends activity { @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_edit_send_to);      // intent activity. every activity has intent ,     // set edittext contents string in info comes     // intent     intent editintent;     edittext etphone;     editintent = this.getintent();     string thephone;     thephone = editintent.getstringextra("current_phone");     etphone = (edittext) this.findviewbyid(r.id.etphone);     etphone.settext(thephone);      // event handler going done button can return     // new message     button btndone = (button) this.findviewbyid(r.id.btndone);     btndone.setonclicklistener(new buttondoneonclickhandler()); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     //getmenuinflater().inflate(r.menu.menu_edit_send_to, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  /**  * handles button done onclick event creating resulting intent ,  * finishing  */ private class buttondoneonclickhandler implements view.onclicklistener {      public void onclick(view v) {          intent intent = new intent();         intent.putextra("new_phone", ((edittext)                 findviewbyid(r.id.etphone)).gettext().tostring());         setresult(result_ok, intent);         finish();     } } } 

editmessage class:

public class editmessage extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_edit_message);         // intent activity. every activity has intent ,         // set edittext contents string in info comes         // intent         intent editintent;         edittext etmessage;         editintent = this.getintent();         string themessage;         themessage = editintent.getstringextra("current_message");         etmessage = (edittext) this.findviewbyid(r.id.etmessage);         etmessage.settext(themessage);          // event handler going done button can return         // new message         button btndone = (button) this.findviewbyid(r.id.btndone);         btndone.setonclicklistener(new buttondoneonclickhandler());     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present. //        getmenuinflater().inflate(r.menu.menu_edit_message, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      /**      * handles button done onclick event creating resulting intent ,      * finishing      */     private class buttondoneonclickhandler implements view.onclicklistener {          public void onclick(view v) {              intent intent = new intent();             intent.putextra("new_message", ((edittext) findviewbyid(r.id.etmessage)).gettext().tostring());             setresult(result_ok, intent);             finish();         }     } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -