logo
down
shadow

ConcurrentModificationException while trying to delete an item from ArrayList


ConcurrentModificationException while trying to delete an item from ArrayList

By : user2177319
Date : October 29 2020, 05:01 AM
I hope this helps . You cannot call remove() from inside a "for-each" loop (the for (item : collection) structure). Doing so will throw that ConcurrentModificationException.
If you want to loop and remove items while looping, you can use a traditional "for" loop:
code :
for (int i = 0; i < itemStorico.size(); ++i) {
    ItemModel itemModel2 = itemStorico.get(i);
    if (...) {
        itemStorico.remove(i);
        ...
    }
}
...
    itemStorico.remove(i);
    --i;
...
for (Iterator<ItemModel> iterator = itemStorico.iterator(); iterator.hasNext(); ) {
    ItemModel itemModel2 = iterator.next();
    if (...) {
        iterator.remove();
        ...
    }
}


Share : facebook icon twitter icon
Delete item from arraylist

Delete item from arraylist


By : 郑雨轩
Date : March 29 2020, 07:55 AM
wish of those help In my programm I have a List of Strings and a List of integer values which should be deleted from the first list. Think of somehing like this: , Sort the deletion list in a descending order and then iterate:
code :
Collections.sort(del, Collections.reverseOrder());
for (Integer toDelete : del) {
    // casting to int, because you need #remove(int), not #remove(Object)
    items.remove((int)toDelete);
}
Remove Random Item From ArrayList Causing ConcurrentModificationException

Remove Random Item From ArrayList Causing ConcurrentModificationException


By : ChrisHazel
Date : March 29 2020, 07:55 AM
this one helps. When you modify subList you are also modifying groupList. So if you are iterating over groupList during this process you would get ConcurrentModificationExceptions.
It doesn't sound like you need to modify groupList (since what you are after is the filteredList). So try making subList it's own list rather than a view over groupList:
code :
List<Group> subList = new ArrayList<>(groupList.subList(firstMatch, lastMatch));
How to delete item from ArrayList in Android

How to delete item from ArrayList in Android


By : Abhinav Gupta
Date : March 29 2020, 07:55 AM
I wish this helpful for you In my application, I have 2 recyclerview into one activity. , According to the exception message:
code :
public void onDelete(int Id) {
    if (usersListSend.size() > 0) {
        usersListSend.remove(Id);
    }

    Toast.makeText(context, "" + Id, Toast.LENGTH_SHORT).show();
}    
public void onDelete(int id) {
    if (id > -1 && id < usersListSend.size()) {
        usersListSend.remove(id);
    }

    Toast.makeText(context, "" + id, Toast.LENGTH_SHORT).show();
}
//Cancel btn
holder.rowSuggestAddUser_cancelImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        deleteItem(position);
        deleteListener.onDelete(model.get(position).getId());
    }
});
//Cancel btn
holder.rowSuggestAddUser_cancelImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int id = model.get(position).getId();
        deleteItem(position);
        deleteListener.onDelete(id);
    }
});
private void deleteItem(int position) {
    model.remove(position);
    notifyItemRemoved(position);
    //notifyItemRangeChanged(position, model.size()); // you notified already and no need to do it again I think
}
@Override
public void onDelete(int Id) {
    if (usersListSend.size() > 0) usersListSend.remove(Integer.valueOf(Id)); // removing by Integer value not index!
    Toast.makeText(context, "" + Id, Toast.LENGTH_SHORT).show();
}
Delete item in Arraylist in RecyclerView within Adapter

Delete item in Arraylist in RecyclerView within Adapter


By : Leonard Davis VIII
Date : March 29 2020, 07:55 AM
wish helps you Actually you are not passing any postID to delete. Check below
Instead of
code :
val postsRef = FirebaseDatabase.getInstance().reference.child("Posts").child("postID")
postsRef.child("postID").removeValue()
val postsRef = FirebaseDatabase.getInstance().reference.child("Posts").child(deletePost.text)
postsRef.removeValue()
delete item in arraylist

delete item in arraylist


By : Sahand
Date : March 29 2020, 07:55 AM
Does that help Using Iterator is good idea (it is thread safe) but you dont need to use interator here because you are not modifying the list as you iterate, also you dont need to sort the list to find the minmum element because that is expensive.
What you have tried is good though dont understand some of your code but try like this:
Related Posts Related Posts :
  • Unexpected token in JSON at position 1024 react native
  • How to make android.net.Uri encode & between query parameters to %26
  • How to simply save image to photo gallery
  • SQLite query to select one row and return it with 2 next rows
  • APK which I developed used to work fine earlier, but suddenly stopped working with "App not installed" error
  • Time Picker Fragments Differentiation
  • ExoPlayer Cant Play some audio streams
  • Start Activity for Result Request Code in Library Project
  • Getting a string value from the strings.xml resource file and set it in an interface class
  • Flutter, sign with both debug keys and release keys on Android
  • will an android service run code from the Xamarin Forms Project even when the app is closed?
  • Change boolean values on listviewitems in firebase
  • Android Studio 3.2.1: mixing versions can lead to runtime crashes when connect firebase
  • FCM migration without losing the old token
  • Change reported Version Number in Fabric Crashlytics
  • lateinit property mMap has not been initialized
  • Android - PWA does not open in standalone mode with service worker
  • Does LeakCanary has callback?
  • How to disable back button after splash screen in React Native
  • Assets Folder Not Including HTML Files In NativeScript Android Build
  • Dagger: What if I *WANT* a new instance every time?
  • How to get mailto working on android chrome?
  • SSO web login in android app with customtabs
  • how to make specific button side curved
  • remove item onlongclick from listview
  • header image alignment coming wrongly in NavigationView android
  • How to add many spaces after the text in TextView
  • how to set "Back Button" , closing activies except main activity in android?
  • Unable to run emulator on Android Studio || Windows 10
  • How can I achieve this requirement using Rx Java
  • How to tilt a Node(or TransformableNode)object downwards in Sceneform(Ar Core)?
  • When to use RTL and LTR?
  • How to create a tab view (category) similar to instagram's explore tab
  • Glide, RecyclerView: Glide returns the same picture over and over again even with different data inputs
  • how to stop open activity while splash screen killed
  • Changing the Color of the Dynamically loaded Buttons onclick
  • Listview: Add arrows as dividers
  • Show confirmation on back/up in Fragment with Navigation Architecture Component
  • How to find the corresponding maven artifact/gradle module of an Android class from the API Docs?
  • ZXing 3.6.0 in portrait mode?
  • Can't use Subversion command line client: svn The path to the Subversion executable is probably wrong android
  • how to set spinner list value first position showing empty
  • When using PersistableBundle in onCreate, it shows me a blank page
  • Failed to resolve Bintray dependency
  • jacoco code coverage not working for Android applications
  • How to change the color of the Android scroll bumper
  • Why run Handler in main Looper thread
  • Android Studio combined chart crashes
  • Setting suggested width and height to a custom view in android
  • How to set input type and format in EditText using kotlin?
  • how to define Android Oncreate optionmenu
  • Exception on orientation change from two-pane mode to one-pane mode
  • How to access members of nested struct and assign them the values read from device tree?
  • How to measure upload progress of a huge String with Retrofit 2?
  • Runtime app localization doesn't work with app bundles
  • How create View and Putting TextView inside it in kotlin
  • how to integrate app action using actions.xml?
  • ConstraintLayout - center TextView in the root but don't override Button
  • Android Studio Text not conforming to layout_margin when I run App on Emulator
  • verifyReleaseResources apk release - react native
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org