Search code examples
androidspinneronitemselectedlistener

Android Spinner onNothingSelected


In my app i have a spinner with 3 values.

If you press the first one it should go to the Activity: UploadPicture

if you press the second one it should go to the Activity: ChangePassword

if you press the last one it should go to the Activity: Login

But since spinner has the first value at launch, there seems no way to trigger the onItemSelected if you pick the first one. (i do not want a forth value with : "please select one" ). So my question is how do i trigger the onNothingSelected? because when i pick the first value it does not trigger the onItemSelected.

My code:

@Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            System.out.println(pos + " " + first);
            if (!first && pos == 0) {

            goUpload();
            } else if (!first && pos == 1) {
                goInternReportActivity();
                //spinner.setSelection(0);

            } else if (!first && pos == 2) {
                goTeksterActivity();
                //spinner.setSelection(0);
            }
            first = false;
        }

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

            goUpload();
        }

Solution

  • I found a workaround solution, where i put a forth element in the String array.

    Like this:

    public View getCustomView(int position, View convertView,
                ViewGroup parent) {
    
            LayoutInflater inflater = getLayoutInflater();
            View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
                    false);
            TextView main_text = (TextView) mySpinner
                    .findViewById(R.id.text_main_seen);
            imageViewLeft = (ImageView) mySpinner.findViewById(R.id.left_pic);
            main_text.setText(spinnerValues[position]);
            if (numberOfElement >= 1) {
                imageViewLeft.setVisibility(View.GONE);
            }
            if (main_text.getText().equals("")){
                main_text.setVisibility(View.GONE);
            }
            System.out.println(numberOfElement + " " + main_text.getText() + " HEJ");
            numberOfElement++;
            return mySpinner;
        }
    

    but i make it invisible, so it wont be showed when the spinner is open. So the first element is empty and contains a image which is showed when the spinner is closed and not open.