Lists are similar to dynamic arrays. You can add and remove items from a list and it will change its size accordingly. A list can hold any type of object. However if a list is declared as a process global object it cannot hold activity objects (like views). Basic4android automatically converts regular arrays to lists. So when a List parameter is expected you can pass an array instead. For example: DimList1AsList List1.Initialize List1.AddAll(ArrayAsInt(1, 2, 3, 4, 5)) Use the Get method to get an item from the list. Lists can be saved and loaded from files using File.WriteList and File.ReadList. You can use a For loop to iterate over all the values: Fori = 0ToList1.Size - 1 DimnumberAsInt number = List1.Get(i)
Adds all elements in the specified collection to the end of the list. Note that you can add an array directly. Example: List.AddAll(ArrayAsString("value1", "value2"))
AddAllAt (IndexAsInt, ListAsList)
Adds all elements in the specified collection starting at the specified index.
Removes all the items from the list.
Get (IndexAsInt) AsObject
Gets the item in the specified index. The item is not removed from the list.
IndexOf (ItemAsObject) AsInt
Returns the index of the specified item, or -1 if it was not found.
Initializes an empty list.
Initializes a list with the given values. This method should be used to convert arrays to lists. Note that if you pass a list to this method then both objects will share the same list, and if you pass an array the list will be of a fixed size. Meaning that you cannot later add or remove items. Example: DimList1AsList List1.Initialize2(ArrayAsInt(1,2,3,4,5)) Example: DimList1AsList DimSomeArray(10) AsString 'Fill array... List1.Initialize2(SomeArray)
InsertAt (IndexAsInt, ItemAsObject)
Inserts the specified element in the specified index, before the current item at that index. As a result all items with index equal to or larger than the specified index are shifted.
Removes the item at the specified index.
Set (IndexAsInt, ItemAsObject)
Replaces the current item in the specified index with the new item.
SizeAsInt [read only]
Returns the number of items in the list.
Sorts the list. The items must all be numbers or strings.
Lexicographically sorts the list, ignoring the characters case. The items must all be numbers or strings.
SortType (FieldNameAsString, AscendingAsBoolean)
Sorts a list with items of user defined type. The list is sorted based on the specified field. FieldName - The case-sensitive field name that will be used for sorting. Field must contain numbers or strings. Ascending - Whether to sort ascending or descending. Example: SubProcess_Globals TypePerson(NameAsString, AgeAsInt)
DimPersonsAsList Persons.Initialize Fori = 1To50 DimpAsPerson p.Name = "Person" & i p.Age = Rnd(0, 121)
Next Persons.SortType("Age", True) 'Sort the list based on the Age field. Fori = 0ToPersons.Size - 1 DimpAsPerson p = Persons.Get(i)
A collection that holds pairs of keys and values. The keys are unique. Which means that if you add a key/value pair (entry) and the collection already holds an entry with the same key, the previous entry will be removed from the map. Fetching an item is done by looking for its key. This is usually a very fast operation (O(1) compared to O(n) in a list). The key should be a string or a number. The value can be any type of object. Note that this map implementation does return items in the same order as they were added. Usually you will use Put to add items and Get or GetDefault to get the values based on the key. If you need to iterate over all the items you can use a For loop like: ForEachKeyAsStringInMap.Keys DimValueAsObject = Map.Get(Key)
Next Similar to a list, a map can hold any object, however if it is a process global variable then it cannot hold activity objects (like views). Maps are very useful for storing applications settings. You can save and load maps with "simple values" with File.WriteMap and File.ReadMap. More complex maps can be saved with B4XSerializator.
Returns the value of the item with the given key. If no such item exists the specified default value is returned.
GetKeyAt (IndexAsInt) AsObject
Returns the key of the item at the given index. GetKeyAt and GetValueAt should be used to iterate over all the items. These methods are optimized for iterating over the items in ascending order. Example: Fori = 0toMap.Size - 1 Log("Key: " & Map.GetKeyAt(i))
Log("Value: " & Map.GetValueAt(i))
GetValueAt (IndexAsInt) AsObject
Returns the value of the item at the given index. GetKeyAt and GetValueAt should be used to iterate over all the items. These methods are optimized for iterating over the items in ascending order. Example: Fori = 0toMap.Size - 1 Log("Key: " & Map.GetKeyAt(i))
Log("Value: " & Map.GetValueAt(i))
Initializes the object. Example: DimMap1AsMap Map1.Initialize
Returns an object which can be used to iterate over all the keys with a For Each block. Example: ForEachkAsStringInmap1.Keys Log(k)
Put (KeyAsObject, ValueAsObject) AsObject
Puts a key/value pair in the map, overwriting the previous item with this key (if such exists). Returns the previous item with this key or null if there was no such item. Note that if you are using strings as the keys then the keys are case sensitive. Example: Map1.Put("Key", "Value")
Remove (KeyAsObject) AsObject
Removes the item with the given key, if such exists. Returns the item removed or null if no matching item was found.
SizeAsInt [read only]
Returns the number of items stored in the map.
Returns an object which can be used to iterate over all the values with a For Each block. Example: ForEachvAsIntInmap1.Values Log(v)