How to set Spinner default value to null?

后端 未结 10 1660
天命终不由人
天命终不由人 2020-11-29 02:36

I\'m trying to get a Spinner to load up with no selected value. Once the user selects a value it then takes them to another page.

This is proving to be a problem be

10条回答
  •  佛祖请我去吃肉
    2020-11-29 03:18

    This is a complete implementation of Paul Bourdeaux's idea, namely returning a special initial view (or an empty view) in getView() for position 0.

    It works for me and is relatively straightforward. You might consider this approach especially if you already have a custom adapter for your Spinner. (In my case, I was using custom adapter in order to easily customise the layout of the items, each item having a couple of TextViews.)

    The adapter would be something along these lines:

    public class MySpinnerAdapter extends ArrayAdapter {
    
        public MySpinnerAdapter(Context context, List items) {
            super(context, R.layout.my_spinner_row, items);
        }
    
        @Override
        public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
            if (position == 0) {
                return initialSelection(true);
            }
            return getCustomView(position, convertView, parent);
        }
    
        @NonNull
        @Override
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
            if (position == 0) {
                return initialSelection(false);
            }
            return getCustomView(position, convertView, parent);
        }
    
    
        @Override
        public int getCount() {
            return super.getCount() + 1; // Adjust for initial selection item
        }
    
        private View initialSelection(boolean dropdown) {
            // Just an example using a simple TextView. Create whatever default view 
            // to suit your needs, inflating a separate layout if it's cleaner.
            TextView view = new TextView(getContext());
            view.setText(R.string.select_one);
            int spacing = getContext().getResources().getDimensionPixelSize(R.dimen.spacing_smaller);
            view.setPadding(0, spacing, 0, spacing);
    
            if (dropdown) { // Hidden when the dropdown is opened
                view.setHeight(0);
            }
    
            return view;
        }
    
        private View getCustomView(int position, View convertView, ViewGroup parent) {
            // Distinguish "real" spinner items (that can be reused) from initial selection item
            View row = convertView != null && !(convertView instanceof TextView) 
            ? convertView :
            LayoutInflater.from(getContext()).inflate(R.layout.my_spinner_row, parent, false);
    
            position = position - 1; // Adjust for initial selection item
            MyModel item = getItem(position);
    
            // ... Resolve views & populate with data ...
    
            return row;
        }
    
    }
    

    That's it. Note that if you use a OnItemSelectedListener with your Spinner, in onItemSelected() you'd also have to adjust position to take the default item into account, for example:

    if (position == 0) {
        return;
    } else {
        position = position - 1;
    }
    MyModel selected = items.get(position);
    

提交回复
热议问题