Add items after some items in ArrayList<Object>

I’m using Kotlin for android.
I get an ArrayList from sql with the follow data, always ordered by path_index:

I’m trying to process that for add specific registers inside the ArraList. I need to put it always in the next row that has the lastRowFromParent equal 1, but if the field lvl of next row is bigger than the current, I have to do that only when the field lvl becomes smaller, adding one register for each previous lastRowFromParent with value 1, as the follow image:

My current code is:

        var antLevel = 0
        var level = 0
        var counterNewRow = 0
        var fullSubItemsList = ArrayList<SubItems>()
        var fullSubItemsSizeAux = 0

        // subItemsList contains all the data from sql, like in first image.

        subItemsList.forEachIndexed { index, subItems ->
            if (!subItems.prioritizeButton) {
                level = subItems.lvl

                fullSubItemsList.add(subItems) 
                if (subItems.lastRowChild) {
                    counterNewRow++
                    if (antLevel > level) {
                        while (counterNewRow>0) {
                            fullSubItemsList.add(
                                index+fullSubItemsSizeAux,   //  I did it to fix index position while the fullSubItemsList is incresing more than subItemsList
                                SubItems(
                                    subItems.parentId,
                                    subItems.lvl,
                                    subItems.path_index.replaceAfterLast('.', "X")
                                ) //it creates an object in that format: id=0, item_name='', parentId=[prev.], lvl=[prev], path_index=[prev], lastRowFromParent=0
                            )
                            fullSubItemsSizeAux++
                            counterNewRow--
                        }
                    }
                }
                antLevel = level
            }
        }

How can I solve that?

this seems to work like you described:

val subItemsList = listOf(
    SubItems(17, "Item 1", 0, 0, "1", 0),
    SubItems(18, "Item 2", 17, 1, "1.1", 0),
    SubItems(20, "Item 3", 18, 2, "1.1.1", 0),
    SubItems(24, "Item 7", 20, 3, "1.1.1.1", 1),
    SubItems(32, "Item 9", 24, 4, "1.1.1.1.1", 1),
    SubItems(33, "Item 10", 32, 5, "1.1.1.1.1.1", 1),
    SubItems(25, "Item 8", 18, 2, "1.1.2", 0),
    SubItems(37, "Item 11", 25, 3, "1.1.2.1", 1),
    SubItems(38, "Item 12", 18, 2, "1.1.3", 1),
    SubItems(21, "Item 4", 17, 1, "1.2", 0),
    SubItems(39, "Item 13", 17, 1, "1.3", 1),
    SubItems(22, "Item 5", 0, 0, "2", 0),
    SubItems(23, "Item 6", 22, 1, "2.1", 1),
    SubItems(87, "Item 18", 0, 0, "3", 0),
)

@Test
fun addChildRows() {
    val result = mutableListOf<SubItems>()
    val memorized = mutableListOf<SubItems>()

    subItemsList.forEach {

        // add memorized items, if lvl decreases
        for (i in memorized.size - 1 downTo 0) {
            if (it.lvl < memorized[i].lvl) {
                result.add(memorized.removeAt(i))
            }
        }

        // add the current item
        result.add(it)

        // create and push new items on the stack, if the current item is the last row
        if (it.lastRowFromParent == 1) {
            memorized.add(
                SubItems(
                    parentId = it.parentId,
                    lvl = it.lvl,
                    path_index = it.path_index.replaceAfterLast(".", "X")
                )
            )
        }
    }

    // flush the remaining items on the stack
    for (i in memorized.size - 1 downTo 0) {
        result.add(memorized[i])
    }

    result.forEach {
        println(it)
    }
}