java - Hi, I am trying to figure out how to pass a string from one class to another -


sorry code. string trying pass contents. let me know if need more info

i new java making silly mistakes. have tried making different methods pass string no luck. if able string appear in edittextbox, great.

i'm new website; sorry if making rookie mistakes.

main class:

package com.app.david.booktracker;    public class mainactivity extends appcompatactivity {      static final string action_scan = "com.google.zxing.client.android.scan";       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         inittoolbar();     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      public void inittoolbar() {          toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         toolbar.settitle("book tracker");         setsupportactionbar(toolbar);       }      public void newbook(view view) {         intent intent = new intent(this, newbook.class);         startactivity(intent);     }      public void showlistbook(view view) {         intent intent = new intent(this, showallbooks.class);         startactivity(intent);     }       public void scanbar(view v) {         try {             intent intent = new intent(action_scan);             intent.putextra("scan_mode", "product_mode");             startactivityforresult(intent, 0);         } catch (activitynotfoundexception e) {             showdialog(mainactivity.this, "no scanner found",                     "download scanner code activity?", "yes", "no").show();         }     }       private static alertdialog showdialog(final appcompatactivity act,                                           charsequence title, charsequence message, charsequence buttonyes,                                           charsequence buttonno) {          alertdialog.builder dowloaddialog = new alertdialog.builder(act);         dowloaddialog                 .settitle(title)                 .setmessage(message)                 .setpositivebutton(buttonyes,                         new dialoginterface.onclicklistener() {                              @override                             public void onclick(dialoginterface dialog,                                                 int which) {                                 uri uri = uri.parse("market://search?q=pname:"                                         + "com.google.zxing.client.android");                                  intent intent = new intent(intent.action_view,                                         uri);                                 try {                                     act.startactivity(intent);                                 } catch (activitynotfoundexception e) {                                 }                              }                         })                 .setnegativebutton(buttonno,                         new dialoginterface.onclicklistener() {                              @override                             public void onclick(dialoginterface dialog,                                                 int which) {                             }                         });          return dowloaddialog.show();     }      @override     protected void onactivityresult(int requestcode, int resultcode, intent intent) {         if(requestcode == 0) {             if(resultcode == result_ok) {                 string contents = intent.getstringextra("scan_result");                 string format = intent.getstringextra("scan_result_format");                 toast.maketext(this,                         "content:" + contents + " format:" + format,                         toast.length_long).show();             }         }     }          @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;         } 

destination class:

package com.app.david.booktracker;  public class newbook extends appcompatactivity {      private final static string tag= "group4.newbook";      private edittext ean;      @override      protected  void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);         setcontentview(r.layout.new_book);         //inittoolbar();     }      @override     public boolean oncreateoptionsmenu(menu menu)     {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }       public void inittoolbar(){         toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         toolbar.settitle("book tracker");          setsupportactionbar(toolbar);          toolbar.setnavigationicon(r.drawable.ic_toolbar_arrow);         toolbar.setnavigationonclicklistener(                 new view.onclicklistener() {                     @override                     public void onclick(view v) {                         intent intent = new intent(v.getcontext(), mainactivity.class);                         startactivity(intent);                     }                 }         );     }        public void newbook(view view)     {         //get         intent intent=new intent(this, mainactivity.class);         edittext edittextbtitle=(edittext) findviewbyid(r.id.edittextfirstname);         edittext edittextbauthor=(edittext) findviewbyid(r.id.edittextsurname);          string booktitle=edittextbtitle.gettext().tostring();         string bookauthor=edittextbauthor.gettext().tostring();          string contents = intent.getstringextra("scan_result");         dataholder.bookadded(booktitle, bookauthor, contents);         log.d(tag, " added: " + booktitle + "  " + bookauthor + " " + contents);          toast.maketext(this, "book added! ", toast.length_long).show();         startactivity(intent);     }  } 

if want able access attributes outside of class, preferred way initialise them private attributes , have "getter" methods. simple method this:

public static string getcontents () {     return contents; } 

will work. however, you'd have initialise variable attribute of class, this:

private static string contents; 

which have declared outside of functions in class (ie. after public static class mainactivity extents appcombatactivity {). don't forget change line initialised string contents contents.

i can't notice, though, you're initialising string of same name in newbook class. you're trying plug in original contents? if so, you'd need do, provided you've done of above, call mainactivity.getcontents(), , should return string you're looking for.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -