list.sort

omaroski

Member
Licensed User
Longtime User
Hello everybody,

i can't make 'list.sort' work, i get an error while i'm running the debug mode.

In practice i want to display a list of contacts in alphabetical order based on what i'm typing into EditText field.

Can you help me?

Thank you!

Omar
:sign0104:

A couple of questions more:
1) In Sub btnSearch_Click, i put the line:

B4X:
ListView1.AddTwoLines2(Contatto(i).DisplayName, PhoneNo.GetKeyAt(p), i)

inside the second for loop without a logical reason, just because it was giving me error both between the two 'Next' and outside the for loops.

2) When i have the app running and i click on one item of the listView it doesn't execute anything inside the Sub ListView1_ItemClick (Position As Int, Value As Object).

B4X:
Sub Globals
   Dim Contatti As Contacts2
   Dim Rubrica As List
   Dim edtName As EditText
   Dim ContattoSingolo As Map
   Dim ListView1 As ListView   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("RicercaContatto")
   ListView1.Initialize("Rubrica")
        Activity.AddView(ListView1, 0, 100, 480, 600)   
End Sub

Sub btnSearch_Click
   ListView1.Clear
   Rubrica = Contatti.FindByName(edtName.Text, False, True, False)
   
   For i = 0 To Rubrica.Size -1
   Dim Contatto(Rubrica.Size) As Contact 
   Contatto(i) = Rubrica.Get(i)
   Dim ListaABC As List
   ListaABC.Initialize2(Contatto)
   ListaABC.Sort(True)
   Dim PhoneNo As Map
   PhoneNo = Contatto(i).GetPhones
   For p = 0 To PhoneNo.Size -1
   
   ListView1.AddTwoLines2(Contatto(i).DisplayName, PhoneNo.GetKeyAt(p), i)
   Next

   Next
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
    Activity.Title = Value
End Sub
 
Last edited:

JonPM

Well-Known Member
Licensed User
Longtime User
You shouldn't Dim and Initialize your list inside the For loop. When you do this you are essentially creating a new object and destroying the previous
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
JonPm is right.

Here's a (non-tested) version of your sub:

B4X:
Sub btnSearch_Click
ListView1.Clear
Rubrica = Contatti.FindByName(edtName.Text, False, True, False)
if Rubrica.size>0 then
    Dim Contatto(Rubrica.Size) As Contact 
    Dim ListaABC As List
    listaABC.initialize
    For i = 0 To Rubrica.Size -1
        Contatto(i) = Rubrica.Get(i)
        dim tempItem as string
        tempItem=Contatto(i).DisplayName
        Dim PhoneNo As Map
        PhoneNo = Contatto(i).GetPhones
        For p = 0 To PhoneNo.Size -1
            listaABC.add(tempItem &  "--" & PhoneNo.GetKeyAt(p) & "--" & i)
        next
    next
    ListaABC.Sort(True)
    for i=0 to listaABC.Size-1
        dim tempFields()
        tempfields=regex.split("--",listaABC.get(i))
        ListView1.AddTwoLines2(tempfields(0),tempfields(1), tempfields(2))
    next
end if
End Sub
I'm not quite happy seeing these two loops, too bad I know nothing about this lib (or class). Also this code might crash with empty fields (name and/or phone number). Still, this can be corrected :)
 
Upvote 0

omaroski

Member
Licensed User
Longtime User
JonPm is right.
I'm not quite happy seeing these two loops, too bad I know nothing about this lib (or class). Also this code might crash with empty fields (name and/or phone number). Still, this can be corrected :)

Actually, with my poor knowledge, i don't know a better way to do this except using 2 for loops. First one is used to create the 'Contatto' array and assign a value at each index, second one has been used cause i couldn't make work the .GetKeyAt() method with a simple Int value inside parenthesis. :sign0148:
Hopefully the code is not crashing with the empty field, it just returns all the contacts into the list!

JonPM said:
You shouldn't Dim and Initialize your list inside the For loop. When you do this you are essentially creating a new object and destroying the previous

Ok, i will look further to avoid this! Thanks!
 
Upvote 0
Top