Android Question Remove customlistview item

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi
I add 10 item to customlistview example below
B4X:
1: for i = 0 to 10
   2: dim p1 as panel
   3: p1.initialize("")
   4: customlistview1.add(p1,100,i)
   5: p1.loadlayout("frm1")
6: next
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
 

ilan

Expert
Licensed User
Longtime 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...



 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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.
 
Upvote 0

ilan

Expert
Licensed User
Longtime 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.

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!!
 
Upvote 0

ilan

Expert
Licensed User
Longtime 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)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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?

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.
 
Upvote 0

ilan

Expert
Licensed User
Longtime 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.

Ok so you have now something todo... waiting for the new class :D
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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

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.
 
Upvote 0

omidaghakhani1368

Well-Known Member
Licensed User
Longtime 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
 
Upvote 0

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
After removing are you refreshing and loading again all views?? (panels, buttons,...)
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?
 
Upvote 0
Top