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?