2

el problema planteado es el siguente , en la actividad tengo un textView y un spinner lo que busco es que cuando se seleccione una opcion en el spinner el valor del textView cambie .

Este es el codigo

public class longitud extends AppCompatActivity {
    TextView tex1, tex2, tex3;

    EditText valor;

    private TypedArray textview;
    private TextView textView;

    private String[] listOfObjects;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_longitud);

        tex1 = (TextView)findViewById(R.id.tex1longitud);
        tex2 = (TextView)findViewById(R.id.tex2longitud);
        tex3 = (TextView)findViewById(R.id.tex3longitud);

        valor = (EditText)findViewById(R.id.edit1longitud);

        listOfObjects = getResources().getStringArray(R.array.object_array3);

        textview = getResources().obtainTypedArray(R.array.object_array_3);
        textView = (TextView) findViewById(R.id.textView1logitud);

        Button btconvertir = (Button)findViewById(R.id.botonlongitud1);

        final Spinner spinner = (Spinner)findViewById(R.id.spinnerlongitud);
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, listOfObjects);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerAdapter);

        btconvertir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (spinner.getSelectedItemPosition()) {
                    case 0 :
                        /**
                         * case EMPTY TO SPINNER TITTLE
                         * **/

                        break;

                    case 1:
                        /** cm to ft
                         *
                         * formula d(ft)=d(cm)/30.42
                         **/

                        if (!(valor.getText().toString()).isEmpty()) {
                            double valor_1 = Double.parseDouble(valor.getText().toString());
                            tex1.setText("d(ft)=d(cm)/30.42");
                            tex2.setText("d(ft)=" + valor_1 + "cm/30.42");
                            double resu = valor_1 / 30.42; /** centimeroFtConstante valor de 30.42 **/
                            tex3.setText("d(ft)=" + valor_1 + "cm");
                        }

                        break;

                    case 2 :
                        /** cm to pulg
                         *
                         * formula d(")=d(cm)/2.54 */

                        if (!(valor.getText().toString()).isEmpty()) {
                            double valor_1 = Double.parseDouble(valor.getText().toString());
                            tex1.setText("d(inc)=d(cm)/2.54");
                            tex2.setText("d(inc)=" + valor_1 + "cm/2.54");
                            double resu1 = valor_1 / 2.54;
                            tex3.setText("d(inc)=" + resu1);
                        }

                        break;

                    case 3 :
                        /**
                         * cm to mm
                         *
                         * formula d(mm)=d(cm)*10
                         * */

                        if (!(valor.getText().toString()).isEmpty()) {
                            double valor_1 = Double.parseDouble(valor.getText().toString());
                            tex1.setText("d(mm)=d(cm)*10");
                            tex2.setText("d(mm)=" + valor_1 + "cm*10");
                            double resu1 = valor_1 * 10;
                            tex3.setText("d(mm)=" + resu1);
                        }

                        break;

                    case 4:
                        /**
                         * ft to cm
                         *
                         * formula d(cm)=d(ft)*30.48
                         * */

                        if (!(valor.getText().toString()).isEmpty()) {
                            double valor_1 = Double.parseDouble(valor.getText().toString());

                            tex1.setText("d(cm)=d(ft)*30.48");
                            tex2.setText("d(cm)=" + valor_1 + "ft*30.48");
                            double resu1 = valor_1 * 30.48;
                            tex3.setText("d(cm)=" + resu1);
                        }

                        break;

                    case 5 :
                        /**
                         * feet to mm
                         *
                         * formula d(mm)=d(ft)*304.8
                         * */

                        if (!(valor.getText().toString()).isEmpty()) {
                            double valor_1 = Double.parseDouble(valor.getText().toString());
                            tex1.setText("d(mm)=d(ft)*304.8");
                            tex2.setText("d(mm)=" + valor_1 + "ft*304.8");
                            double resu1 = valor_1 * 305.8;
                            tex3.setText("d(mm)=" + resu1);
                        }

                        break;

                    case 6:
                        /**
                         * feet to inches
                         *
                         * formula d(inch)=d(ft)*12
                         * */

                        if (!(valor.getText().toString()).isEmpty()) {
                            double valor_1 = Double.parseDouble(valor.getText().toString());
                            tex1.setText("d(inch)=d(ft)*12");
                            tex2.setText("d(inch)=" + valor_1 + "ft*12");
                            double resu1 = valor_1 * 12;
                            tex3.setText("d(inch)=" + resu1);
                        }

                        break;

                    case 7 :
                        if (!(valor.getText().toString()).isEmpty()) {
                            double valor_1 = Double.parseDouble(valor.getText().toString());
                        }

                        break;

                    default:
                        break;
                }
            }
        });

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                textView.setText(textview.getResourceId(spinner.getSelectedItemPosition(), -1));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}

3 respuestas 3

1

Hola prueba realizando el siguiente cambio en tu codigo :

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        //aqui ya teniendo la posicion del objeto seleccionado vamos a buscar su valor a la lista que cargo el spinner :)
        textView.setText( listOfObjects[position] );


    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});

Espero te sirva. Saludos

1
  • gracias con ese me pude guiar Commented el 17 abr. 2017 a las 16:40
1

En lugar de un OnClickListener en el botón,

btconvertir.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (spinner.getSelectedItemPosition()) {

debes configurar un OnItemSelectedListener en el spinner:

final Spinner spinner = (Spinner)findViewById(R.id.spinnerlongitud);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, listOfObjects);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch(position) {
            case 0:
                /**
                 * case EMPTY TO SPINNER TITTLE
                 **/

                break;

            case 1:
                /** cm to ft
                 *
                 * formula d(ft)=d(cm)/30.42
                 **/

                if (!(valor.getText().toString()).isEmpty()) {
                    double valor_1 = Double.parseDouble(valor.getText().toString());
                    tex1.setText("d(ft)=d(cm)/30.42");
                    tex2.setText("d(ft)=" + valor_1 + "cm/30.42");

                    double resu = valor_1 / 30.42; /** centimeroFtConstante valor de 30.42 **/
                    tex3.setText("d(ft)=" + valor_1 + "cm");
                }

                break;
            case 2:
                /** cm to pulg
                 *
                 * formula d(")=d(cm)/2.54 */
                if (!(valor.getText().toString()).isEmpty()) {
                    double valor_1 = Double.parseDouble(valor.getText().toString());
                    tex1.setText("d(inc)=d(cm)/2.54");
                    tex2.setText("d(inc)=" + valor_1 + "cm/2.54");
                    double resu1 = valor_1 / 2.54;
                    tex3.setText("d(inc)=" + resu1);
                }

                break;
            case 3:
                ...

                break;

            case 4:
                ...

                break;
            default:
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});
0

Encontré la solución a mi problema. Funcionó de esta forma:

private String[]  listOfObjects2;

listOfObjects2 = getResources().getStringArray(R.array.object_array_3);

textView.setText( listOfObjects2[position] );

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.