android - call back function java -
i have class called buycoins, within class have method addlisteneronspinneritemselection()
public void addlisteneronspinneritemselection() { spinner1 = (spinner) findviewbyid(r.id.spinner1); textview t=(textview) findviewbyid(r.id.conversion); customonitemselectedlistener c = new customonitemselectedlistener(t); spinner1.setonitemselectedlistener(c); //string stockcode=c.getstock(); //log.d(tag,"message"); }
this creates new object detects item(product) selected on spinner. pass buyclass. have attempted lines commented out, value receive null.
public class customonitemselectedlistener implements onitemselectedlistener { ... public string stock; ... public void onitemselected(adapterview<?> parent, view view, int pos, long id) { string selected=parent.getitematposition(pos).tostring(); switch(selected) { case "20 coins": toast.maketext(parent.getcontext(), "onitemselectedlistener : " + selected, toast.length_short).show(); t.settext("$20"); this.setstock("20coins"); break; ... } private void setstock(string s) { stock=s; } public string getstock() { return stock; } }
the problem you're querying listener right after adding it, i.e. event can't possibly have happend yet. since don't know when happening (if @ all) you'll need decouple that.
example (simple pseudo code, started):
class model { string stock; } class listener { model model; listener( model m) { model = m; } //in case component might spinner or parent, depending on rest of code void onitemselected( component c ) { m.stock = c.gettext(); } }
and when create , register listener:
class buycoins { model model; //initialize ... void initlisteners() { spinner.addlistener( new listener(model) ); } }
Comments
Post a Comment