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?

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);              


    }



}

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

1 Respuestas

You should add listener for spinner

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

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

                 outputTextview.setText(str);

            }

            @Override
            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.