Home > Software engineering >  How to remove item from list in Android
How to remove item from list in Android

Time:10-13

In my application I have 2 list and maybe some items same between lists.
I want to check these lists and remove same items.
I write below codes but after run application show me error and crashed application!
My codes:

if (ALLERGIC_LIST.isNotEmpty()) {
    ALLERGIC_LIST.forEach { allergic ->
        //Added to user list
        userLocalList.add(LocalDataModel(allergic.faName.toString(), true, 0, allergic.id!!))
        userRecyclerView.adapter!!.notifyDataSetChanged()
        //Remove from temp list
        dataListTemp.forEach { temp ->
            Log.e("AllergicLogs","${allergic.id} - ${temp.id}")
            if (allergic.id == temp.id) {
                dataListTemp.remove(temp)
            }
        }
    searchList.adapter!!.notifyDataSetChanged()
    }
}

Error message in Logcat:

2022-10-13 08:44:07.617 11990-11990/com.myapp E/AllergicLogs: 1791538835 - 1749914577
2022-10-13 08:44:07.617 11990-11990/com.myapp E/AllergicLogs: 1791538835 - 1762590770
2022-10-13 08:44:07.617 11990-11990/com.myapp E/AllergicLogs: 1791538835 - 1791538835
2022-10-13 08:44:07.617 11990-11990/com.myapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp, PID: 11990
    java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.next(ArrayList.java:831)
        at com.myapp.AllergicFragment.onViewCreated$lambda-14$lambda-7(AllergicFragment.kt:233)
        at com.myapp.AllergicFragment.$r8$lambda$VH4SurM8Hl44Zx-Iy-J2iPtdh8g(AllergicFragment.kt)
        at com.myapp.AllergicFragment$$ExternalSyntheticLambda3.onChanged(D8$$SyntheticClass)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at androidx.lifecycle.LiveData$1.run(LiveData.java:93)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6121)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

How can I fix it and remove same item?

CodePudding user response:

dataListTemp.forEach { temp ->
            Log.e("AllergicLogs","${allergic.id} - ${temp.id}")
            if (allergic.id == temp.id) {
                dataListTemp.remove(temp)// Here is the error. You're doing concurrent modification here
                dataListTempDuplicate.remove(temp)//Instead delete from duplicate array
            }
        }

CodePudding user response:

dataListTemp = dataListTemp.filter { dataItem->
    ALLERGIC_LIST.find {it.id == dataItem.id} == null
} 

Filter removes all items that don't return true from the lambda. The find function will check to see if there's an item with that id in the allergic list, or return null if not. So if find returns a match, we return false in the filter lambda and skip that item. If we don't, we return true from the lambda and keep it in the filtered list.

This replaces the entire outer foreach loop. Runtime will be O(n^2), but do was the original.

  • Related