Filter out double entries in (column of) a Table

BasicBert

Member
Licensed User
Longtime User
Hi all,

My question is not about a specific B4A feature, but a programming question in general. But since I need it in a B4A-app, this seems to be the right place to ask.

I have a table with 10 columns and about 135 rows (and expanding slowly).
In the first column of each row a text (string) exists.

I would like to load a spinner with the different values in column 0, but if a value already exists in the spinner it should not be added again.

I can go over all rows in the table and add column 0 to the spinner, but that adds everything in Column 0 to the spinner :

B4X:
For i = 0 To Tabel.LastEntry 'Go over all entries in Table called Tabel.
   spnPlaatsInvoer.Add(Tabel.GetValue(0,i)) 'add it to the spinner
Next

I need some way to skip adding a value when it already exists in the spinner.

:sign0163:

Later I might want to sort the Spinner-entries on the number of occurences or just alphabetically, but for now that's not necessary.
 

mc73

Well-Known Member
Licensed User
Longtime User
I have two question: a) how are you populating your array? From a sqlite db?
and b) what will be the purpose of the spinner? Some sort of category?
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Use a map:

Loop through your table
Add column1 to the map as the key and its index as the value

Then loop through the map
Add each key to the spinner - you can use the index as needed

This works because a map cannot contain multiple keys that are the same, so only the last one is used. If you want to use the first one then loop through the table backwards.
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
As MC73 hints, if your table entries are growing a SQLite table might make sence and them populating the spinner with unique results would be simple with a Select Distint query i.e.

SELECT DISTINCT 'Fieldname' FROM 'Tablename'

Douglas
 
Upvote 0

BasicBert

Member
Licensed User
Longtime User
Thanks for the replies.
Using a map looks fine, although I have no experience with that. But now is the time to learn and use it also for other purposes.

The table is populated from a csv file, not a sql database. It will not grow fast, only one, sometimes two entries per week.



Sent from my HTC OneX with Tapatalk.
 
Upvote 0

BasicBert

Member
Licensed User
Longtime User
:sign0142:Using a Map as described by Kickaha does the trick very well.
I'm also able to count the number of occurences of a key and use that number as the associated value of the key using the following code :

B4X:
Sub SetSpinner
   spnPlaatsInvoer.Clear
   Dim Teller As Int = 1
   Dim TempMap As Map
   Dim sKey As String
   TempMap.initialize
   For i = 0 To Tabel.LastEntry
      sKey = Tabel.GetValue(0,i)
      If TempMap.ContainsKey(sKey) Then 'Key exists
         Teller = TempMap.Get(sKey) 'Get number of occurences
         Teller = Teller + 1 'add one occurence
         TempMap.Put(sKey,Teller) 'put it in the map
      Else
         Teller = 1 'New key
         TempMap.Put(sKey,Teller)'put it in the map
      End If
   Next
'
'Sort the map on the values ???
'
   For i = 0 To TempMap.Size - 1 'Populate the spinner
      spnPlaatsInvoer.Add(TempMap.GetKeyAt(i))
   Next
   End Sub

Later I might want to sort the Spinner-entries on the number of occurences or just alphabetically, but for now that's not necessary.
Now, sorting the map on the number of occurences would be very nice, just to get the most used at the beginning of the spinner's population. No need for an alphabetical sort, though.

Getting the map into a list and then sort the list would be a way, but seems overdoing it. Is there any way to sort the map directly?
 
Upvote 0
Top