Android Question delete some items from list

Discussion in 'Android Questions' started by PassionDEV, May 25, 2019.

  1. PassionDEV

    PassionDEV Active Member Licensed User

    i have the followig list and i want to delete the whole list and leave the last 4 items was inserted in the list based on some condition , is it possible ?

    Code:
    dim Type cmdline(command as string)
    dim CommandList As List

    if CommandList.Size  > 50 then

    For i = 0 To CommandList.Size - 1
    Dim cmd As cmdline
    cmd = CommandList.Get(i)
    'here i want to delete all items that contain something and leave the last 4 items inserted how can i possibly do that ?
    If cmd.command = "something" Then
    CommandList.RemoveAt(i)
    End If


    Next

    End if
     
  2. OliverA

    OliverA Well-Known Member Licensed User

    Not a solution, just a tip. You need to start from the top of the list and work down when you are removing items from the list, otherwise you will not get the results you want.
     
    PassionDEV and mangojack like this.
  3. PassionDEV

    PassionDEV Active Member Licensed User

    i have started the loop from top but i am not sure on how to skip the last 4 items in the list while removing

    Code:
    if CommandList.Size  > 50 then

    For i =  CommandList.Size -1 To 0 Step -1
    Dim cmd As cmdline
    cmd = CommandList.Get(i)
    If cmd.command = "something" Then
    CommandList.RemoveAt(i)
    End If


    Next

    End if
     
  4. mangojack

    mangojack Well-Known Member Licensed User

    Are you referring to the last 4 items that were added to the list ? .. then
    Code:
    For i =  CommandList.Size -5 To 0 Step -1
     
  5. DonManfred

    DonManfred Expert Licensed User

    PUT ON your brain! It is simple MATH.

    CommandList.Size -1 is the last item.
    CommandList.Size -1-4 is 4 items less then the last.

    Code:
    For i =  CommandList.Size -5 To 0 Step -1
     
    mangojack and PassionDEV like this.
  6. PassionDEV

    PassionDEV Active Member Licensed User

    i was asking about the last 4 items that equal to same condition "something" because this list will have multiable items with a different commands name skipping the last 4 items in the total list will remove the last 4 items if they equal the condition "something" but what if the last 4 items are mixed with different names

    i think i should calculate each loop with a global integer and count each "something" condition in order to target those conditions only i know that i should set size- if i want to skip this order in list i got misunderstood
     
  7. DonManfred

    DonManfred Expert Licensed User

    Untested but i think it should bring your the in the right direction!?

    Code:
    If CommandList.Size  > 50 Then
            
    Dim toRemove As List
            toRemove.Initialize
            
    For i =  CommandList.Size -1 To 0 Step -1
                
    Dim cmd As cmdline
                cmd = CommandList.Get(i)
                
    If cmd.command = "something" Then
                    
    'CommandList.RemoveAt(i)
                    toRemove.Add(i)
                
    End If
            
    Next
            
    '
            If toRemove.Size>4 Then
                
    For i = toRemove.Size -5 To 0 Step -1
                    CommandList.RemoveAt(toRemove.Get(i))
                
    Next
            
    End If
        
    End If
     
    PassionDEV likes this.
  8. emexes

    emexes Well-Known Member Licensed User

    Code:
    'here i want to delete all items that contain something and leave the last 4 items inserted how can i possibly do that ?
    Do While CommandList.Size > 4
        CommandList.RemoveAt(
    0)
    Loop
     
  9. OliverA

    OliverA Well-Known Member Licensed User

    If your list items are
    1,9,2,9,3,9,4,9,5,9,9,9,9
    And the item 9 is what you are looking for, then should the end result be
    1,2,3,4,5,9,9,9,9
    Or
    1,9,2,9,3,9,4,9,5
    Or
    9,9,9,9 and it is all the 9’s before the 5
    Or
    9,9,9,9 and it is all the 9’s after the 5
     
    PassionDEV and mangojack like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice