How to set my first value (default value) in spinn

2019-08-07 03:14发布

How to set my first value (default value) in spinner as empty. Means, until the user selects value from list, spinner will have no value. I tried below

list.add(""); 

But, it won't work. Below is my code :

if(values.contains("options")){
       spinner.setOnItemSelectedListener(MyClass.this);
       List<String> list = new ArrayList<String>();
       list.add("");
       list.add("Test");
       list.add("Working");
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, list) {};

      adapter.setDropDownViewResource(R.layout.custom_spinner_list);
      spinner.setAdapter(adapter);     
   }
 }

2条回答
你好瞎i
2楼-- · 2019-08-07 04:08

i found out the way. Here is how we do it, let the last line be

list.add("");

and add below code :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, list) {

                        @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 don't display last item. It is used as hint.
                            }

                    };


                    adapter.setDropDownViewResource(R.layout.custom_spinner_list);
                    spinner.setAdapter(adapter);
                    spinner.setSelection(adapter.getCount());        //set the hint the default selection so it appears on launch.
                    spinner.setOnItemSelectedListener(this);
                    notifyDataSetChanged();        
查看更多
我只想做你的唯一
3楼-- · 2019-08-07 04:16

Thanks to user45678's answer above, I wrote this

public class SpinnerNoDefault extends Spinner {

public void initialize(List<String> items) {
    initialize(items, android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_dropdown_item);
}

public void initialize(List<String> items, @LayoutRes int mItemResId, @LayoutRes int dropdownResId) {
    List<String> itemsWithEmpty = new ArrayList<>(items);
    itemsWithEmpty.add("");

    String[] choicesArray = itemsWithEmpty.toArray(new String[itemsWithEmpty.size()]);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getContext(), mItemResId, choicesArray) {
        @NonNull
        @Override
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            if (position == getCount()) {
                ((TextView)v.findViewById(android.R.id.text1)).setText(null);
                ((TextView)v.findViewById(android.R.id.text1)).setHint(parent.getContext().getString(R.string.spinner_no_default_hint));
            }

            return v;
        }

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

    spinnerArrayAdapter.setDropDownViewResource(dropdownResId);
    setAdapter(spinnerArrayAdapter);
    setSelection(itemsWithEmpty.size()-1);
}
}
查看更多
登录 后发表回答