ArrayAdapter.add-->java.lang.UnsupportedOperationException

sourcecode:

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方法添加spinneritem时,抛出java.lang.UnsupportedOperationException...

rootcause:ArrayListsize固定,导致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<?extendsString>collection),需API11及以上提供支持

相关推荐