Android Question ListView and Index

luke2012

Well-Known Member
Licensed User
Longtime User
Hi to all,
I need to keep the alignment between a listview and a map using the index as key.

The problem is that each time I have to do an item update I must remove the item and re-add the item to the list because there isn't an update method in the ListView object.

For Example:

B4X:
            lsv.RemoveAt(Index)
            lsv.AddTwoLinesAndBitmap(Data, "", Bitmap)

The problem is that the indexes change each time I make an item "update" within the listview and I need to keep the indexes (keys) aligned between the listview and the Map.

So my problem is to apply the same ListView index rule to the Map (keys).

So suppose to remove item1 from the listview (suppose a size of 6 items) and add a new item assuming that the new item have the same content with a small addition:

Old item removed: "item1" (index is 3)
New item added: "item1 +" (index is 5) ---> it always assume lsv.size-1 index value

What happen to the others items indexes ? The items before the removed item keep their indexes and the item between the removed item and the last added item will be decreased by 1? Is this right?
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
What data does the Map contain? is there another piece of data you could use as the map key to link the list item to the map and use the AddTwoLinesAndBitmap2 method to add it to the list, so that it doesn't matter where in the list the item is, you can still get at the map as the value is returned in the click sub.

You could even generate a sequential key yourself.
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
What data does the Map contain? is there another piece of data you could use as the map key to link the list item to the map and use the AddTwoLinesAndBitmap2 method to add it to the list, so that it doesn't matter where in the list the item is, you can still get at the map as the value is returned in the click sub.

You could even generate a sequential key yourself.

The map contains a key that is composed by the index and related data as value. So the index is the unique key that link items with the related value in the map.

For example:

LISTVIEW

item1 (0)
item2 (1)
item3 (2)
item4 (3)

MAP

Key1 (0$$$Item_Category) ---> List1 (Value1)
Key2 (1$$$Item_Category) ---> List2 (Value2)
Key3 (2$$$Item_Category) ---> List3 (Value3)
...........

So the index is the piece of key that link uniquely the Map's Value with the Item within the ListView (index).

Note that in item within the Listview could have a duplicated name so the item content (text) isn't unique.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The map Key is the Key, it's data (contents) is something else.

Do the contents of the map change? or just the pointers to each element .
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
The content (values) of the MAP may change.

But the real problem is that the link between the item and the linked MAP Key is broken if the item index* change (items index is managed dinamically within the listview object).

*as index a mean the "Position" passed within the _ItemClick listview event

To solve this problem (for me) there are two solutions:

1) Implement a sub that update the stored keys within the MAP based on the items index changes (for ex. when you remove an item the other items indexes may change)
2) Implemente a kind of GUID (item unique identifier) that always keep the link between item and the related MAP key (in this case the item should store the GUID within itself).
 
Last edited:
Upvote 0
Top