7

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!

AlphaVladim
  • 101
  • 1
  • 1
  • 8

4 Answers4

9

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;
            }
        });
Javier Roberto
  • 1,293
  • 11
  • 17
3

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.

Alejandro Cumpa
  • 2,118
  • 1
  • 24
  • 45
  • 1
    "I need it to collect data when i click the spinner". Please read my post! I don't need OnItemSelectedListener. Also, GPU_LAYOUT is the main ScrollView. – AlphaVladim Sep 20 '13 at 08:37
  • Maybe you should do something llike this: public static class CustomSpinner extends Spinner{ private OnSpinnerEventsListener mListener; @Override public boolean performClick() {mOpenInitiated = true; if (mListener != null) {//here capture the data you want mListener.onSpinnerOpened();} return super.performClick();} public void setSpinnerEventsListener(OnSpinnerEventsListener OnSpinnerEventsListener) { mListener = onSpinnerEventsListener; } public void performClosedEvent() { mOpenInitiated = false; if (mListener != null) { mListener.onSpinnerClosed(); } } } – Alejandro Cumpa Sep 20 '13 at 10:39
0
min_max.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

    //whatever your logic is put it here 
 }
}); 
Furqan
  • 465
  • 6
  • 22
0

you can't use setOnClickListener directly on a Spinner. If you want to do it here is a trick.

 @Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (view.getId()) {
        case R.id.sp_category:
            if (motionEvent.getAction() == MotionEvent.ACTION_UP)//replacing with onclick
            {
                //Perform any action
            }
            break;
    }
    return true;
}

In this code MotionEvent.ACTION_UP will make this touch act like onclick. Hope it will help you!

Ehsan Rafique
  • 107
  • 1
  • 7