Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 256
AlphaVladim Avatar asked Sep 20 '13 07:09

AlphaVladim


2 Answers

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;
            }
        });
like image 74
Javier Roberto Avatar answered Oct 18 '22 13:10

Javier Roberto


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.

like image 33
Alejandro Cumpa Avatar answered Oct 18 '22 13:10

Alejandro Cumpa