Setting a spinner onClickListener() in Android

后端 未结 7 736
不思量自难忘°
不思量自难忘° 2020-11-29 03:43

I\'m trying to get an onClickListener to fire on a Spinner, but I get the following error:

Java.lang.RuntimeException is \"Don\'t call setOnClickListe

相关标签:
7条回答
  • 2020-11-29 04:11

    The following works how you want it, but it is not ideal.

    public class Tester extends Activity {
    
        String[] vals = { "here", "are", "some", "values" };
        Spinner spinner;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            spinner = (Spinner) findViewById(R.id.spin);
            ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, vals);
            spinner.setAdapter(ad);
            Log.i("", "" + spinner.getChildCount());
            Timer t = new Timer();
            t.schedule(new TimerTask() {
    
                @Override
                public void run() {
                    int a = spinner.getCount();
                    int b = spinner.getChildCount();
                    System.out.println("Count =" + a);
                    System.out.println("ChildCount =" + b);
                    for (int i = 0; i < b; i++) {
                        View v = spinner.getChildAt(i);
                        if (v == null) {
                            System.out.println("View not found");
                        } else {
                            v.setOnClickListener(new View.OnClickListener() {
    
                                @Override
                                public void onClick(View v) {
                                            Log.i("","click");
                                            }
                            });
                        }
                    }
                }
            }, 500);
        }
    }
    

    Let me know exactly how you need the spinner to behave, and we can work out a better solution.

    0 讨论(0)
提交回复
热议问题