Selecting different spinner option doesn't update calculatio

Put your problem here if it does not fit any of the other categories.

Selecting different spinner option doesn't update calculatio

Postby brisk » Sat Mar 10, 2012 2:50 pm

Hey guys,

I have a simple spinner which has several number options. I want to just select a number and then it does some math to that number and outputs it in a text box. For some reason it's only doing math on the 1st entry in the list and if you change it doesn't update.

Any ideas?

Code: Select all
package placeorder.com;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Spinner;
import android.widget.TextView;

public class Time extends Activity{   
   
   double totalhours, cost;
   int price;
   TextView total, orderid;   
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.time);
      
      Spinner spinhours = (Spinner) findViewById(R.id.sp_hours);
      total = (TextView) findViewById(R.id.tv_total);   
      orderid = (TextView) findViewById(R.id.tv_orderid);   
      
      Random order = new Random();
      int randomorder = order.nextInt(9999);      
      order.nextInt(9999);            
      orderid.setText("Order ID: "+randomorder);      
      
      price = 5;                
      String hours = spinhours.getSelectedItem().toString();
      totalhours = Integer.parseInt(hours);      
      cost = totalhours * price;            
      total.setText("£" + cost);            
      
      
   }


   
}
brisk
Freshman
Freshman
 
Posts: 6
Joined: Sat Mar 10, 2012 2:44 pm

Top

Re: Selecting different spinner option doesn't update calcul

Postby Phyll » Sat Mar 10, 2012 6:00 pm

Hi brisk,

I see that the whole activity takes place in the onCreate(). That means that your calculating part calculates the one that is selected from the beginning (the 1st entry in the list ) and nothing more. You need to have a listener for the spinner so that it gets called when the selection changes. That will call your calculation (which should be in its own method so you can call it from anywhere) and change the displayed output.

I don't think I have any example right now. If you can't figure it out, I'll see if I can't come back and put something up here like what you are trying to do.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Selecting different spinner option doesn't update calcul

Postby brisk » Sat Mar 10, 2012 6:03 pm

Thank you for you reply!!

This is what I have at the moment...everything works apart from when I select a number in the list with a .5 so 1.5 or 2.5, it just then just force closes. The other whole integers like 1,2,3,4,5 all calculate n display fine.

Code: Select all
package placeorder.com;

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Time extends Activity {
   
   double totalhours, cost;
   int price;
   TextView orderid, total;
   String[] hourslisted;
   Random order = new Random();
      

   @Override
   public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.time);      
      
      orderid = (TextView) findViewById(R.id.tv_orderid);
      total = (TextView) findViewById(R.id.tv_total);
      int randomorder = order.nextInt(9999);                     
      orderid.setText("Order ID: " + randomorder);
         
      hourslisted = getResources().getStringArray(R.array.hours);
      Spinner spinhours = (Spinner) findViewById(R.id.sp_hours);      
         
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_spinner_dropdown_item, hourslisted);
      
      spinhours.setAdapter(adapter);
      spinhours.setOnItemSelectedListener(new OnItemSelectedListener()
      
      {
         
         public void onItemSelected(AdapterView<?> arg0, View arg1,
         int arg2, long arg3)
         {
            price = 5;
            String str = (String) arg0.getSelectedItem();
            totalhours = Integer.parseInt(str);
            cost = totalhours * price;
            total.setText("£" + cost);
                
      }
      public void onNothingSelected(AdapterView<?> arg0) {}      });
      
      
   }

}
brisk
Freshman
Freshman
 
Posts: 6
Joined: Sat Mar 10, 2012 2:44 pm

Re: Selecting different spinner option doesn't update calcul

Postby brisk » Sat Mar 10, 2012 6:04 pm

Thank you for you reply!!
brisk
Freshman
Freshman
 
Posts: 6
Joined: Sat Mar 10, 2012 2:44 pm

Re: Selecting different spinner option doesn't update calcul

Postby Phyll » Sat Mar 10, 2012 6:21 pm

Hi brisk,

Great, You don't suppose the Integer.parseInt() has anything to do with that do you?

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Selecting different spinner option doesn't update calcul

Postby brisk » Sat Mar 10, 2012 6:22 pm

Phyll wrote:Hi brisk,

Great, You don't suppose the Integer.parseInt() has anything to do with that do you?

Hope this helps.

Phyll


YES I JUST CHANGED THAT HA, HENCE THE DELETED POST ;)

THANKS PHYLL WHOEVER YOU ARE, PEACE MAN

ED
brisk
Freshman
Freshman
 
Posts: 6
Joined: Sat Mar 10, 2012 2:44 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 18 guests