spinner with no select option

[亡魂溺海] 提交于 2019-12-13 02:22:31

问题


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.



来源:https://stackoverflow.com/questions/11013434/spinner-with-no-select-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!