Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.UnsupportedOperationException android arraylist

Tags:

android

list

i am getting unsupported operation exception when i remove item from list.

new AlertDialog.Builder(getActivity())
                        .setMessage(result.getString("Message"))
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                miPaymentModel.remove(index);
                            })
                        .show();

}

stack trace is here

01-11 16:42:37.610: E/AndroidRuntime(25930): FATAL EXCEPTION: main
01-11 16:42:37.610: E/AndroidRuntime(25930): java.lang.UnsupportedOperationException
01-11 16:42:37.610: E/AndroidRuntime(25930):    at java.util.AbstractList.remove(AbstractList.java:638)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at com.paksoft.people.Fragment.MiPaymentFragment$DeleteCardAccount$1.onClick(MiPaymentFragment.java:395)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at android.os.Looper.loop(Looper.java:137)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at android.app.ActivityThread.main(ActivityThread.java:4499)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at java.lang.reflect.Method.invokeNative(Native Method)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at java.lang.reflect.Method.invoke(Method.java:511)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
01-11 16:42:37.610: E/AndroidRuntime(25930):    at dalvik.system.NativeStart.main(Native Method)
like image 583
Muhammad Aamir Ali Avatar asked Nov 30 '22 02:11

Muhammad Aamir Ali


1 Answers

On Arrays.asList returning a fixed-size list

From the Class API Documentation here :

Arrays.asList: Returns a fixed-size list backed by the specified array.

You can't add to it; you can't remove from it. You can't structurally modify the List.

To support this type of operation

Create a LinkedList, which supports faster remove.

List<String> list = new LinkedList<String>(Arrays.asList(split));

Ref Answer: https://stackoverflow.com/a/2965808/2123594 and credits are goes to @polygenelubricants

like image 114
Swapnil Sonar Avatar answered Dec 05 '22 01:12

Swapnil Sonar