I have a spinner with drop down mode . I display my list of items with my custom adapter in that. But now i want to use a hint text for my spinner that is unselectable. How can i do this ? thanks in advance
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You have to add first item like i did in my code then on click of spinner it will hide so user will not able to select that first item
ArrayList cityArraList = new ArrayList();
cityArrayList.add("Select City");
Spinner citySpinner = (Spinner) findViewById(R.id.citySpinner);
final ArrayAdapter<City> cityAdapter = new ArrayAdapter<City>(getActivity(),R.layout.my_simple_list_item_1,cityArrayList){
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = null;
// If this is the initial dummy entry, make it hidden
if (position == 0) {
TextView tv = new TextView(getContext());
tv.setHeight(0);
tv.setVisibility(View.GONE);
v = tv;
} else {
// Pass convertView as null to prevent reuse of special case views
v = super.getDropDownView(position, null, parent);
}
// Hide scroll bar because it appears sometimes unnecessarily, this does not prevent scrolling
//parent.setVerticalScrollBarEnabled(false);
return v;
}
};