How to set Spinner default value to null?

后端 未结 10 1649
天命终不由人
天命终不由人 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:27

    My solution, in case you have a TextView as each row of the spinner:

        // TODO: add a fake item as the last one of "items"
        final ArrayAdapter adapter=new ArrayAdapter(..,..,items) 
          {
          @Override
          public View getDropDownView(final int position,final View convertView,final ViewGroup parent)
            {
            final View dropDownView=super.getDropDownView(position,convertView,parent);
            ((TextView)dropDownView.findViewById(android.R.id.text1)).setHeight(position==getCount()-1?0:getDimensionFromAttribute(..,R.attr.dropdownListPreferredItemHeight));
            dropDownView.getLayoutParams().height=position==getCount()-1?0:LayoutParams.MATCH_PARENT;
            return dropDownView;
            }
          }
    
        ...
        spinner.setAdapter(adapter);
        _actionModeSpinnerView.setSelection(dataAdapter.getCount()-1,false);
    
    
    
      public static int getDimensionFromAttribute(final Context context,final int attr)
        {
        final TypedValue typedValue=new TypedValue();
        if(context.getTheme().resolveAttribute(attr,typedValue,true))
          return TypedValue.complexToDimensionPixelSize(typedValue.data,context.getResources().getDisplayMetrics());
        return 0;
        }
    

提交回复
热议问题