Search code examples
javaandroidspinneronclicklistener

Android - Spinner + setOnClickListener


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!


Solution

  • I'm doing it like this:

    public void addListenerOnSpinnerItemSelection() {
            mySpinner = (Spinner) findViewById(R.id.GPU_LAYOUT);  
            mySpinner.setOnItemSelectedListener(new myOnItemSelectedListener());
        }
    
    public class myOnItemSelectedListener implements OnItemSelectedListener {       
    @Override
    public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long arg3)     {           
    
    int position=Arrays.asList(getResources().getStringArray(R.array.GPU_LAYOUT)).indexOf(fecha);           }
    
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {        }       
    }
    

    Also I suggest to get the position of the group you're using and then handle it.