Android: la selección de una opción de ruleta diferente no actualiza el cálculo en TextView, solo funciona en el primer elemento de la lista

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.

¿Alguna idea?


import java.util.Random;

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;    

    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        Spinner spinhours = (Spinner) findViewById(;
        total = (TextView) findViewById(; 
        orderid = (TextView) findViewById(; 

        Random order = new Random();
        int randomorder = 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);              



preguntado el 10 de marzo de 12 a las 14:03

1 Respuestas

You should add listener for spinner

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                 String str = (String) arg0.getSelectedItem();



            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub


respondido 10 mar '12, 14:03

My spinner has integers in it not strings, how do I fetch an integer rather than a string? - Ed Briscoe

int i = Integer.parseInt(string); - Samir Mangrolia

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.