2

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

3 Answers3

2

I once had same issue and wanted to use onNothingSelected callback.

I discovered that we cannot use onNothingSelected and onItemSelected together if item at index 0 also has some method like yours.

As per documentation Spinner onItemSelectedcallback is invoked only when:

  1. the newly selected position is different from the previously selected position

  2. if there was no selected item.

So, index 0 item is always selected even if you don't touch your spinner. Even it's not what you want to do: A fourth value "Select Action" in index 0 is best solution to solve your problem.

Community
  • 1
  • 1
Crawler
  • 1,988
  • 3
  • 21
  • 42
1

As spinner always select first item when activity creates . Here you need to set flag for first time activity launch. try that code .

int a = 1, b = 0;

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {

                  if (b < a) 
                  {
                          b++;
                   } 
                  else 
                  {
                     Toast.makeText(context, String.valueOf(parent.getItemAtPosition(position)) , Toast.LENGTH_LONG).show();
                  }
            }
            public void onNothingSelected(AdapterView<?> parent) {
                return;
            }
        });

onNothingSelected is not a right way as you want .

Sats
  • 875
  • 6
  • 12
1

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.