android - getting value from two spinner into string variables -


i'm trying pass strings "año" , "artista" 2 spinners next activity , value "año" "artista" void. used method values in other activity , there works here, "artista" void. moreover, have tried switch method , without success.

i hope helps me , tells me i'm doing wrong. read , information before ask help,

this code,

public class buscar extends activity implements onitemselectedlistener{      string año;     string artista;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_buscar);         buscar();          spinner spinnerbuscaraño = (spinner) findviewbyid(r.id.añospinner);         string[] añobuscar = {"1995", "2000", "2005"};         spinnerbuscaraño.setadapter(new arrayadapter<string>(this, android.r.layout.simple_spinner_dropdown_item, añobuscar));         spinnerbuscaraño.setonitemselectedlistener(this);          spinner spinnerbuscarartista = (spinner) findviewbyid(r.id.artistaspinner);         string[] artistabuscar = {"michael jackson", "u2", "depeche mode"};         spinnerbuscarartista.setadapter(new arrayadapter<string>(this, android.r.layout.simple_spinner_dropdown_item, artistabuscar));         spinnerbuscarartista.setonitemselectedlistener(this);      }      /*      public void onitemselected(adapterview<?> parent, view view,int pos, long id) {         switch (parent.getid()) {             case r.id.añospinner:                 año = parent.getitematposition(pos).tostring();                 break;             case r.id.artistaspinner:                 artista = parent.getitematposition(pos).tostring();                 break;             default:                 break;         }     }      */      @override     public void onitemselected(adapterview<?> parent, view v, int position,                                long id){         // todo auto-generated method stub          spinner spinner = (spinner) parent;         if(spinner.getid() == r.id.añospinner)         {             año = parent.getitematposition(position).tostring();         }         else if(spinner.getid() == r.id.artistaspinner)         {             artista = parent.getitematposition(position).tostring();         }      }      @override     public void onnothingselected(adapterview<?> arg0) {         // todo auto-generated method stub     }      public void buscar() {          button buttonbuscar = (button) findviewbyid(r.id.buscar);          buttonbuscar.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view arg0) {                  toast.maketext(getapplicationcontext(),artista, toast.length_short).show();                   intent intentbuscar = new intent(buscar.this, listadomusica.class);                 intentbuscar.putextra("myaño", año);                 intentbuscar.putextra("myartista", artista);                 startactivity(intentbuscar);               }         });      }  } 

if can me,

thanks in advance,

possibly trying catch string of intent name.

try code:

spinnertest.java

public class spinnertest extends activity implements adapterview.onitemselectedlistener {       string year;     string artist;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.spinner_activity);          spinner spinneryear = (spinner) findviewbyid(r.id.spinner1);         final string[] years = {"1995", "2000", "2005"};         spinneryear.setadapter(new arrayadapter<string>(this, android.r.layout.simple_spinner_dropdown_item, years));         spinneryear.setonitemselectedlistener(this);          spinner spinnerartists = (spinner) findviewbyid(r.id.spinner2);         final string[] artists = {"michael jackson", "u2", "depeche mode"};         spinnerartists.setadapter(new arrayadapter<string>(this, android.r.layout.simple_spinner_dropdown_item, artists));         spinnerartists.setonitemselectedlistener(this);          button btnsearch = (button) findviewbyid(r.id.btnsearch);         btnsearch.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 intent searchintent = new intent(spinnertest.this, searchactivity.class);                 searchintent.putextra("year", year);                 searchintent.putextra("artist", artist);                 startactivity(searchintent);              }         });     }      @override     public void onitemselected(adapterview<?> adapterview, view view, int i, long l) {         switch (adapterview.getid()){             case r.id.spinner1:                 year = adapterview.getselecteditem().tostring();                 toast.maketext(spinnertest.this, year, toast.length_long).show();                 break;             case r.id.spinner2:                 artist = adapterview.getselecteditem().tostring();                 toast.maketext(spinnertest.this, artist, toast.length_long).show();                 break;         }      }      @override     public void onnothingselected(adapterview<?> adapterview) {      } } 

searchactivity.java:

public class searchactivity extends actionbaractivity {      string year;     string artist;       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_receive_spinner_value);         textview txtyear = (textview) findviewbyid(r.id.txtyear);         textview txtartist = (textview) findviewbyid(r.id.txtartist);          if(getintent().hasextra("year")){             year = getintent().getstringextra("year");             txtyear.settext(year);         }         if(getintent().hasextra("artist")){             artist = getintent().getstringextra("artist");             txtartist.settext(artist);         }     }   } 

it works me


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -