getSelectedItemPosition() of a spinner always returns 0

左心房为你撑大大i 提交于 2019-12-10 17:14:21

问题


I'm trying to make the alert box show the selected item on the spinner and I'll need the position later on the code. I tested and the spiAli.getSelectedItemPosition() only returns 0 even before the switch. Here is my string:

private static final String[] listaAlimentos =
        {"Arroz","Feijão","Bife"};
ArrayAdapter<String> alistaAlimentos;    

And the function that's not working:

 butFinalizar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            String escolhaAlimento = null;
            String escolhaExercicio = null;

            if (cbAlimento.isChecked()) {
                int selected = spiAli.getSelectedItemPosition();
                switch (selected) {
                    case (0):
                        escolhaAlimento = "Arroz";
                        break;
                    case (1):
                        escolhaAlimento = "Feijão";
                        break;
                    case (2):
                        escolhaAlimento = "Bife";
                        break;
                }
                AlertDialog.Builder dialogo = new
                        AlertDialog.Builder(MainActivity.this);
                dialogo.setTitle("Aviso");
                dialogo.setMessage("Escolha:" + escolhaAlimento);
                dialogo.setNeutralButton("OK", null);
                dialogo.show();
            }

        }
    });

来源:https://stackoverflow.com/questions/28137029/getselecteditemposition-of-a-spinner-always-returns-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!