spinner with no select option

2019-08-03 23:15发布

问题:

I'm trying to create spinner which should not have any select but instead of it, it should show Blank, after clicking that items can be selected.

Here is my code, please help.

urineGlucoseSpinner =  (Spinner) view.findViewById(R.id.spnner_urine_glucose);

    ArrayList<String> ugList = new ArrayList<String>();
    ugList.add(0,"");
    ugList.add("1.5");
    ugList.add("5.5");
    ugList.add("0.8");
    ugList.add("9.5");
    ugList.add("12.0");

    //ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
    ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text, ugList);
    urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
    urineGlucoseSpinner.setSelection(0);
    urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());

回答1:

By default spinner takes array 0th element if u not selecting any one..u have to make object of ArrayList and for 0th element u have to put "" (null Sting) inside semicolon and make it as 0th element...i think this is the only solution for your question..

 ArrayList<String> ugList = new ArrayList<String>();
 ugList.add("");


回答2:

I can see two ways to do this.

1) Add the blank line to your data at position 0, and then create a custom spinner adapter and override the getView method and in it use an if to set the 0 position view to GONE (thus getting rid of the blank line in the listing).

2) An alternative might be setting an empty EditText in your form, and when it gains focus pop a listview in a dialog with your possible choices.