android - multiplying 2 spinner values together -


string.xml

  <string-array name="fruits">        <item>apple</item>        <item>banana</item>        <item>orange</item>        <item>pear</item>        <item>watermelon</item>        <item>mango</item>        <item>pineapple</item>        <item>strawberry</item> </string-array>  <string-array name="total">     <item>1</item>     <item>2</item>     <item>3</item>     <item>4</item>     <item>5</item>     <item>6</item>     <item>7</item>     <item>8</item> </string-array>  <string-array name="calorie">     <item>80</item>     <item>101</item>     <item>71</item>     <item>100</item>     <item>45</item>     <item>135</item>     <item>80</item>     <item>53</item> </string-array>     

java file:

    public class fruit extends activity implements onclicklistener, onitemselectedlistener { private textview tvfruit, tvno; context context=this;    @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.fruit);      tvfruit = (textview) findviewbyid(r.id.tvfruit);     tvno = (textview) findviewbyid(r.id.tvno);       final spinner fruits = (spinner)findviewbyid(r.id.spin_fruit);     arrayadapter<charsequence> adapter = arrayadapter.createfromresource(             this,r.array.fruits,android.r.layout.simple_spinner_item);     adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);     fruits.setadapter(adapter);     fruits.setonitemselectedlistener(this);      fruits.setonitemselectedlistener(new onitemselectedlistener(){     public void onitemselected(adapterview<?> arg0, view arg1, int arg2,     long arg3) {             // todo auto-generated method stub             string selecteditem = fruits.getselecteditem().tostring();             tvfruit.settext(selecteditem);         }      public void onnothingselected(adapterview<?> arg0) {         // todo auto-generated method stub      }      });      final spinner num = (spinner)findviewbyid(r.id.spin_no);     arrayadapter<charsequence> adapter1 = arrayadapter.createfromresource(             this,r.array.total,android.r.layout.simple_spinner_item);     adapter1.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);     num.setadapter(adapter1);     num.setonitemselectedlistener(this);      num.setonitemselectedlistener(new onitemselectedlistener(){         public void onitemselected(adapterview<?> arg0, view arg1, int arg2,         long arg3) {                 // todo auto-generated method stub                 string selecteditem = num.getselecteditem().tostring();                 tvno.settext(selecteditem);             }          public void onnothingselected(adapterview<?> arg0) {             // todo auto-generated method stub          }          });      button save = (button) findviewbyid(r.id.bsave);     save.settextcolor(color.blue);     save.setonclicklistener(new view.onclicklistener() {               public void onclick(view arg0) {                 // todo auto-generated method stub                 // subject details , show in alertdialog                 alertdialog.builder builder = new alertdialog.builder(context);                 builder.setmessage("success");                 builder.setpositivebutton("ok", null);                 builder.setnegativebutton("view log", new dialoginterface.onclicklistener(){                      public void onclick(dialoginterface dialog, int which) { // part done negative button                                                                               // if want link new intent                         launchintent();                     }                  });                 builder.create().show();             }              //making "view log" button in dialog box go new intent fruitlog.class             private void launchintent(){                 intent = new intent(fruit.this, fruitlog.class);                 i.addflags(intent.flag_activity_clear_top);                 startactivity(i);             }           });      } 

so when user click 3 bananas in spinner, want know how code multiplication of calories. once done calculation, should show 303 calories in "total calorie" textview

so when user click 3 bananas in spinner, want know how code multiplication of calories. once done calculation, should show 303 calories in "total calorie" textview

can guide me on how coding calculation. example, me 2 apple calories , rest try figure out.thank much. community have being helpful.

using existing code, try along line of this:

first, change calorie array integer-array. next, add:

public int calculatecalories() {     int[] calorie = getresources().getintarray(r.array.calorie);     return integer.parseint((string) num.getselecteditem()) * calorie[fruits.getselecteditemposition()]; } 

this function should return number of chosen fruit times caloric value. explicit set directly textview so:

totaltextview.settext(string.valueof(calculatecalories())); 

you have make num , fruits visible entire class, declaring them same way tvfruit declared.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -