1

Begginer android programer,我想添加和刪除字符串列表中的項目,當用戶點擊listitem,但我得到操作不支持錯誤試圖做到這一點。請參閱以下代碼以瞭解我想要做什麼。添加或刪除列表<String>時不支持操作?

@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()); 
      } 
     } 
} 

這裏是logcat的

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) 
+0

你可以把logcat消息? – Shubhayu 2012-04-08 05:48:59

回答

4

如果myList中有名單的數據類型,它是不可改變的。您無法對其進行更改。所以你會得到操作不支持異常。嘗試進行更改之前,將myList數據類型更改爲ArrayList

+1

謝謝你解決問題。 – 2012-04-08 06:01:49

相關問題