在我的应用程序中,我想为arrayList添加一些值,我希望添加的项目将是arrayList的第一项.我用这个代码
Sphere[] spheres = new ObjectMapper().readValue(response.getJSONArray("data").toString(),Sphere[].class);
mSphereList = Arrays.asList(spheres);
Sphere sphere = new Sphere();
sphere.setId(0);
sphere.setTitle("All");
mSphereList.add(0,sphere);
mListView.setAdapter(new CategoryAdapter(CategoryActivity.this,mSphereList));
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,我得到例外
ERROR/AndroidRuntime(25878): FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么.你能告诉我一个好的解决方案吗?
Arrays.asList()不返回ArrayList.它返回一个固定大小的列表,由原始数组支持(如文档所述).
你必须创建一个真正的ArrayList:
mSphereList = new ArrayList<>(Arrays.asList(spheres));
Run Code Online (Sandbox Code Playgroud)