Android Question Remove customlistview item

Discussion in 'Android Questions' started by omidaghakhani1368, Jul 22, 2015.

  1. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    Hi
    I add 10 item to customlistview example below
    Code:
    1for i = 0 to 10
       
    2dim p1 as panel
       
    3: p1.initialize("")
       
    4: customlistview1.add(p1,100,i)
       
    5: p1.loadlayout("frm1")
    6next
    when i remove item from 10 items,and again remove item then get error that not exist index
    i use index from line 4 variable i
     
  2. ilan

    ilan Expert Licensed User

    if you try to remove like this: cls.RemoveAt(10)

    then you will get an error and the reason is because in first place you add 11 items and every item you enter a specific value that is the i from the loop
    now every items index (place in the list) is the same like the value "i", that will stay like this if you wont delete any item but if you delete now index 7 that is in pos 7
    your list would look like this

    item1: value=0 (index or position in list is 0)
    item2: value=1 (index or position in list is 1)
    item3: value=2 (index or position in list is 2)
    item4: value=3 (index or position in list is 3)
    item5: value=4 (index or position in list is 4)
    item6: value=5 (index or position in list is 5)
    item7: value=6 (index or position in list is 6)
    item8: value=8!!!! (index or position in list is 7)
    item9: value=9 (index or position in list is 8)
    item10: value=10 (index or position in list is 9)


    so if you try to remove item with index 10 you will get an error because your list has now only until index 9

    so you will need to search for an item that store that value and remove it...



     
  3. LucaMs

    LucaMs Expert Licensed User

    Sorry, I don't understand clearly.

    You can remove an Item using RemoveAt(ItemIndex).

    ItemIndex must be a number between 0 (not 1) and CustomListView1.GetSize - 1.

    After you removed an item, CustomListView1.GetSize will be decremented by one, of course.
     
  4. ilan

    ilan Expert Licensed User

    what i believe is that he is mixing the real index position and the value "i" that he has added in the first loop
    he need to understand that every time he removes items the index will be different from the value he stored at the first loop

    because in first loop he has an item with value 10 that is also in index 10 but after delete an item this item will still have value 10 but will be in index 9!!
     
    mangojack likes this.
  5. LucaMs

    LucaMs Expert Licensed User

    Probably, with a few modifications, you can use a Map instead of a List (I'm talking about the internal List of the class) and then give the opportunity to manage the Item by "name" (name can be any object).
     
  6. ilan

    ilan Expert Licensed User

    and what will happen if i have the same name in 2 items??

    there MUST be a uniq value for each item like the position (index)
     
  7. LucaMs

    LucaMs Expert Licensed User

    you can't :D
     
  8. ilan

    ilan Expert Licensed User

    Name is something i should choose or have i understood wrong

    You sugess to search for an item by his name that is something you add to make it easier for you to remember, right?
     
  9. LucaMs

    LucaMs Expert Licensed User

    Yes, it could be a name but also a number (to avoid the problem of the first post).

    Simply, you could have a different Add routine (or Add2), like:

    Add(... Name as Object)

    and you check if that Name already exists.
     
    ilan likes this.
  10. ilan

    ilan Expert Licensed User

    Ok so you have now something todo... waiting for the new class :D
     
  11. LucaMs

    LucaMs Expert Licensed User

    ... but I don't know CustomListView well (never used) so I'm reading and it has a Value parameter in the routine Add ... I'm reading :D
     
  12. LucaMs

    LucaMs Expert Licensed User

    Surely, so will I multiply the donations received; they will go from 0 to 0 * 100 :D
     
  13. ilan

    ilan Expert Licensed User

    100 likes...;)
     
  14. ilan

    ilan Expert Licensed User

    Maybe you can sell those likes here like people sell likes for facebook:D
     
    LucaMs likes this.
  15. LucaMs

    LucaMs Expert Licensed User

    If I'm not mistaken, that parameter, Value (which is an Int) is a value associated to an item.
    So (when I am awake) we could change its type to Object and create a function to get the item by that object.

    Maybe, because I'm still reading the class.
     
  16. LucaMs

    LucaMs Expert Licensed User

    ... uhm... but if you add thousands of items...
    being the search sequential, it would take a long time; using a Map it would be much faster.
     
  17. LucaMs

    LucaMs Expert Licensed User

    I'm seeing things; before to me that Value seemed an Int, now an Object!

    I'm sleepy.:D
     
  18. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    Thank you my friends.
    no no no you wasn't understand.
    assume i have 10 item in customlistview
    now i remove item index 4 from list and it removed and again remove index item 4 and it return error
    i use index from line 4 for position item because i load different layout that contain button and when i click on button should be remove item
     
  19. ilan

    ilan Expert Licensed User

    After removing are you refreshing and loading again all views?? (panels, buttons,...)
     
  20. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    I save index of each panel in button tag
    when i click on button i get button tag (it is index) and then remove item with index
    but after remove item,count of item in list minus 1 and not exist index
    how i load all views?your meaning is load all item again?
     
Loading...