I crashed today so I'll leave it as a memo
fun releaseGroupItem(uri: Uri){
var i = 0
selectImageList.forEach {
if (it.imageUri == uri){
selectImageList.removeAt(i)
}
i++
}
val adapter = SelectImageFragmentAdapter(supportFragmentManager, selectImageList)
select_image_pager.adapter = adapter
indicator_default.setViewPager(select_image_pager)
}
This will cause a crash with java.util.ConcurrentModificationException
when this method is called consecutively.
This is a well-known story in Java, and I'm usually addicted to it when I'm just starting programming. (I've written the code that causes this phenomenon after 2 years of Android ...)
~~ If the cause is ArrayList # remove, it will not be deleted considering the order of the list. It is nicely summarized below. ~~ (added below) http://programming-study.com/technology/java-list-remove/
fun releaseGroupItem(uri: Uri){
val iterator = selectImageList.iterator()
while (iterator.hasNext()){
val removeUri = iterator.next()
if (removeUri.imageUri == uri) {
iterator.remove()
}
}
val adapter = SelectImageFragmentAdapter(supportFragmentManager, selectImageList)
select_image_pager.adapter = adapter
indicator_default.setViewPager(select_image_pager)
}
Use ʻiterator. This one seems to have better performance, so it may be better to basically use ʻiterator
except that the element to be deleted is simple and only called once.
It's an empty article, but I'll leave it as a personal note.
I told you in the comment section!
https://docs.oracle.com/javase/jp/8/docs/api/java/util/ArrayList.html
Iterator simply allows you to add and remove elements in the List in the for statement, but ArrayList throws an exception when you do that.
Thank you for teaching!
Recommended Posts