Android - Spinner + setOnClickListener

AlphaVladim picture AlphaVladim · Sep 20, 2013 · Viewed 12.9k times · Source

So, i want to assign a Spinner OnClick, but i get the following error

     java.lang.RuntimeException: Don't call   setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead

I need it to collect data when i click the spinner, then use it later. I know i did it before, but i lost the code. Googled for an answer, i didn't find anything that works. If this helps, I am using pagerview layout. Here is my code:

     public class DATA extends Fragment {
         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (container == null) {
            return null;
        }

  ScrollView GPU_LAYOUT = (ScrollView)inflater.inflate(R.layout.gpu, container, false);

      final TextView higher_than =(TextView) GPU_LAYOUT.findViewById(R.id.gpu_higher_than_value);

        final Spinner min_max =(Spinner) GPU_LAYOUT.findViewById(R.id.min_max_spinner_gpulay);  

        min_max.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String TMP=min_max.getSelectedItem().toString();
                higher_than.setText(TMP);
            }
        });

       return GPU_LAYOUT;
      }

Is there any way i can do it? Thank you!

Answer

Javier Roberto picture Javier Roberto · Dec 11, 2013

You can do it with setOnTouchListener in that way:

spinner.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Toast.makeText(getBaseContext(), "CLICK", Toast.LENGTH_SHORT).show();
                return false;
            }
        });