当所选项目是提示时,不会调用Spinner onItemSelected

时间:2016-09-13 10:01:03

标签: android spinner onitemclicklistener

我有一个自定义微调器。

@覆盖     public void setOnItemSelectedListener(final OnItemSelectedListener listener){

@IBAction func tapLA(sender: UIButton) {
    openPicturesFrom(contentImagesA)
}

func openPicturesFrom(arr: Array) {
    //here you have array and you can start opening pictures
}

当所选项目是提示时,不会调用Spinner onItemSelected。 所以当用户选择spinner提示onItemSelected时不要调用。

那么我如何处理微调器中的提示选择?

2 个答案:

答案 0 :(得分:0)

    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0) {
                Toast.makeText(getActivity(), "its your hint ", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getActivity(), "do here what you want ", Toast.LENGTH_SHORT).show();
            }
        }


        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

答案 1 :(得分:0)

试试这个。

Spinner spinner_subcate = (Spinner) findViewById(R.id.subcat_spin);    

ArrayAdapter dataAdapter1 = new ArrayAdapter(MainActivity.this,R.layout.spinner_item){

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {

                        View v = super.getView(position, convertView, parent);
                        if (position == getCount()) {
                            ((TextView)v.findViewById(android.R.id.text1)).setText("");
                            ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
                        }

                        return v;
                    }

                    @Override
                    public int getCount() {
                        return super.getCount()-1; // you dont display last item. It is used as hint.
                    }

                };

                dataAdapter1.setDropDownViewResource(R.layout.spinner_dropdown_item);

                dataAdapter1.add("list item1");
                dataAdapter1.add("list item2");
                dataAdapter1.add("list item3");
                dataAdapter1.add("hint");

                // Spinner click listener
                spinner_subcate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        String sub_cat = parent.getItemAtPosition(position).toString();
                        Log.d("sub cat::",sub_cat);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

                // attaching data adapter to spinner
                spinner_subcate.setAdapter(dataAdapter1);
                spinner_subcate.setSelection(dataAdapter1.getCount()); //set the hint the default selection so it appears on launch.