1

初学者 android 程序员,我想在用户单击 listitem 时从字符串列表中添加和删除项目,但是在尝试执行此操作时出现操作不支持错误。请参阅以下代码以了解我想要做什么。

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if(myList != null)
        {
            StringBuilder stringBuilder = new StringBuilder();
            if(myList.contains(mylist1.get(position).getPackageName()))
            {
                for(int i = 0; i < myList.size(); i++)
                {
                    if(myList.get(i).equals(mylist1.get(position).getPackageName()))
                        continue;
                    if((myList.size() - 1) != i)
                        stringBuilder.append(myList.get(i)).append(separator);
                    else
                        stringBuilder.append(myList.get(i));
                }
                editor.putString("mypreference", stringBuilder.toString());
                mylistt1.get(position).setDisabled(false);
       //error happens in following
                myList.remove(mylist1.get(position).getPackageName());
            }
      }
}

这是日志猫

04-08 11:33:00.030: ERROR/AndroidRuntime(533): FATAL EXCEPTION: main
        java.lang.UnsupportedOperationException
        at java.util.AbstractList.remove(AbstractList.java:638)
        at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
        at java.util.AbstractCollection.remove(AbstractCollection.java:229)
        at com.mypackage.myActivity.onListItemClick(myActivity.java:82)
        at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
        at android.widget.AdapterView.performItemClick(AdapterView.java:292)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
        at android.widget.AbsListView$1.run(AbsListView.java:3168)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4340)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

4

如果 myList 的数据类型为List,则它是不可变的。您无法对其进行更改。所以你会得到 操作不支持的异常。在尝试进行更改之前将myList 数据类型更改为ArrayList 。

于 2012-04-08T05:55:51.143 回答