source code:
cities = new String[] { "Android", "BlackBerry", "J2ME","Symbian","Broncho", "LinuxMobile", "Palm", "WindwosMobile" };
cityList = Arrays.asList(cities);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,cityList);
situation:
利用ArrayAdapter.add方法添加spinner item时,抛出java.lang.
UnsupportedOperationException...
root cause:
ArrayList size固定,导致ArrayAdapter也是resizeable
solutions:
1.在创建ArrayAdapter时,不设置固定size的ArrayList
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
cities = new String[] { "Android", "BlackBerry", "J2ME","Symbian","Broncho", "LinuxMobile", "Palm", "WindwosMobile" };
for(int i=0; i<cities.length; i++){
arrayAdapter.add(cities[i]);
}
2.利用
arrayAdapter.addAll(String...items)或者
arrayAdapter.addAll(Collection<? extends String> collection),需API11及以上提供支持