Reputation: 553
I'm programming on Kotlin.
I get an ArrayList (of SubItems object) 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>() //variable where I'm trying to write the objects as in second image
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 do that?
Upvotes: 0
Views: 69
Reputation: 841
You need to change your existing approach to rely on parentId column to identify your .X rows. Here is a step by step algorithm:
Maintain a set and a stack of parentIds.
Read each parentId and check if it is in the set:
Case No - Not in set, add it to set and push on stack
Case Yes - It is in set, keep popping from stack till you reach the parentId. Keep removing the popped id from set and keep adding ".X" to your list.
Here is a dry run:
parentId 0 - add to stack, add current record to your list
parentId 17 - add to stack, add current record to your list
parentId 18 - add to stack, add current record to your list
parentId 20 - add to stack, add current record to your list
parentId 24 - add to stack, add current record to your list
parentId 32 - add to stack, add current record to your list
parentId 33 - add to stack, add current record to your list
parentId 53 - add to stack, add current record to your list
parentId 54 - add to stack, add current record to your list
parentId 55 - add to stack, add current record to your list
parentId 18 - already in stack, start popping until you reach 18. Pop 55, 54, 53, 33, 32, 24 and 20 add them to your list with ".X". Then add current record to your list
parentId 25 - add to stack, add current record to your list
parentId 18 - already in stack, start popping until you reach 18. Pop 25, add it to your list with ".X". Then add current record to your list
parentId 17 - already in stack, start popping until you reach 17. Pop 18, add it to your list with ".X". Then add current record to your list
parentId 17 - already in stack, start popping until you reach 17. No items to pop so add current record to your list
parentId 0 - already in stack, start popping until you reach 0. Pop 17, add it to your list with ".X". Then add current record to your list
parentId 22 - add to stack, add current record to your list.
parentId 0 - already in stack, start popping until you reach 0. Pop 22, add it to your list with ".X". Then add current record to your list
parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
parentId 0 - already in stack, start popping until you reach 0. No items to pop so add current record to your list
Add "X" record when end.
Upvotes: 1