好的,所以。我完全被这个问题难住了。我有一个函数,它对对象列表进行排序并将其返回,例如 1、1.1、1.2、1.2.1、2、2.2、3。
fun getFolderHierachy(allFolders: List<FolderEntity>): List<FolderEntity>? {
//puts the entrypoints( 1, 2, 3) in a list
if (allFolders.isNotEmpty()) {
val foldersSorted: MutableList<FolderEntity> = mutableListOf()
allFolders.forEach {
if (it.parentDir == null) {
foldersSorted.add(it)
}
}
// should insert an element, after the matching one ( 1, 1.1, 2, 3)
while (foldersSorted.size != allFolders.size) {
foldersSorted.forEach { parentFolder ->
allFolders.forEach {
if (!foldersSorted.contains(it)) {
if (it.parentDir == parentFolder.uid) {
val index = foldersSorted.indexOf(parentFolder)
foldersSorted.add(index + 1, it)
}
}
}
}
}
println(foldersSorted)
return foldersSorted
}
return null
}
我知道应该工作,因为它已经为我提供了正确的结果。但是,在我的主人最后一次更新之后,它抛出了这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.ur.mi.audidroid, PID: 16634
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.next(ArrayList.java:860)
at de.ur.mi.audidroid.viewmodels.FolderViewModel.getFolderHierachy(FolderViewModel.kt:206)
at de.ur.mi.audidroid.viewmodels.FolderViewModel$initFolderSorting$1.onChanged(FolderViewModel.kt:45)
at de.ur.mi.audidroid.viewmodels.FolderViewModel$initFolderSorting$1.onChanged(FolderViewModel.kt:20)
at androidx.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:152)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
at androidx.lifecycle.LiveData$1.run(LiveData.java:91)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
该更新甚至没有更改与此功能相关的任何内容。我知道这与迭代时列表的更改有关,但 MutableList 不应该处理这个问题吗?我对如何更改它以使其再次工作感到很困惑,请帮忙。