java - Variables to appear in TextView -
unfortunately, none of strings appear in textview
have created. i'm looking have 2 textviews; 1 cost
, other time
.
mainactivity.java
public class mainactivity extends activity { int inttime = 30; int intcost = 10; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); checkbox cp = (checkbox)findviewbyid(r.id.checkpepperoni); checkbox cs = (checkbox)findviewbyid(r.id.checksausage); checkbox cb = (checkbox)findviewbyid(r.id.checkbacon); checkbox cm = (checkbox)findviewbyid(r.id.checkmushroom); final imageview cheese =(imageview)findviewbyid(r.id.imgcheese); final imageview pepperoni =(imageview)findviewbyid(r.id.imgpepperoni); final imageview sausage =(imageview)findviewbyid(r.id.imgsausage); final imageview bacon =(imageview)findviewbyid(r.id.imgbacon); final imageview mushroom =(imageview)findviewbyid(r.id.imgmushroom); final sharedpreferences sharedpref =preferencemanager.getdefaultsharedpreferences(this); cp.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener(){ //pepperoni listener @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked1) { // todo auto-generated method stub if(ischecked1){ pepperoni.setimageresource(r.drawable.pepperoni); inttime = inttime + 6; intcost = intcost + 5; } else{ pepperoni.setimageresource(0); inttime = inttime - 6; intcost = intcost - 5; } }}); cs.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener(){ //sausage listener @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked2) { // todo auto-generated method stub if(ischecked2){ sausage.setimageresource(r.drawable.sausage); inttime = inttime + 6; intcost = intcost + 5; } else{ sausage.setimageresource(0); inttime = inttime - 6; intcost = intcost - 5; } }}); cb.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener(){ @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked3) { // todo auto-generated method stub if(ischecked3){ bacon.setimageresource(r.drawable.bacon); inttime = inttime + 6; intcost = intcost + 5; } else{ bacon.setimageresource(0); inttime = inttime - 6; intcost = intcost - 5; } }}); cm.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener(){ @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked4) { // todo auto-generated method stub if(ischecked4){ mushroom.setimageresource(r.drawable.mushroom); inttime = inttime + 6; intcost = intcost + 5; } else{ mushroom.setimageresource(0); inttime = inttime - 6; intcost = intcost - 5; } }}); imagebutton ib = (imagebutton)findviewbyid(r.id.imgbtngo); ib.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub string time = integer.tostring(inttime); string cost = integer.tostring(intcost); sharedpreferences.editor editor = sharedpref.edit(); editor.putstring("keytime", time); editor.putstring("keycost", cost); startactivity(new intent(mainactivity.this, resultactivity.class)); } }); } }
resultactivity.java
public class resultactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_result); textview time = (textview)findviewbyid(r.id.txttime); textview cost = (textview)findviewbyid(r.id.txtcost); sharedpreferences sharedpref = preferencemanager.getdefaultsharedpreferences(this); string strtime = sharedpref.getstring("keytime", ""); string strcost = sharedpref.getstring("keytime", ""); cost.settext("cost" + (strcost)); time.settext("and " + strtime + " deliver."); } }
anybody have advise? need have strcost , strtime show in textview
in resultactivity.java.
make sure commit when working editor:
sharedpreferences.editor editor = sharedpref.edit(); editor.putstring("keytime", time); editor.putstring("keycost", cost); editior.commit();
Comments
Post a Comment