Get the Text of selected Listview-Item

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Helo,

how getting the Text of selected Listview-Item?

grüße aus Berlin
sinan
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Sinan,

this might help.

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim details, cName, cphone As String
   Dim listOfContacts As List
   Dim lvphone As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
   getcontacts
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub getcontacts 
   Dim Contacts2 As Contacts2
   listOfContacts.Initialize
   listOfContacts = Contacts2.GetAll(True,False)
   'Log(listOfContacts)
   
     'listOfContacts.Sort(True)
   'Log(listOfContacts)
      
   For i = 0 To listOfContacts.Size - 1 
       Dim Contact As Contact 
       Contact = listOfContacts.Get(i) 
      'Log(Contact) 'will print the fields to the LogCat 
  
      If Contact.PhoneNumber <>"" Then  
         'If there Is a Contact number pass it To the ListView so you can see it 
                         
         'lvphone.TwoLinesLayout.SecondLabel.textcolor=Colors.blue
         lvphone.SingleLineLayout.Label.TextSize = 20 
         lvphone.SingleLineLayout.ItemHeight = 70 
         lvphone.AddSingleLine(Contact.DisplayName &CRLF &Contact.PhoneNumber)
         'lvphone.AddTwoLines(Contact.DisplayName,Contact.PhoneNumber) 
         lvphone.ScrollingBackgroundColor =Colors.DarkGray 
       End If 
   Next   

End Sub

Sub lvphone_ItemClick (Position As Int, Value As Object)  As String
    
    details = Value 
    'Log("******************************************")
    'Log("selected " & details)  'the name in the logs As a check 
    
   'Log("Break found at " & details.IndexOf(CRLF))
   'Log("******************************************")
   cName=details.SubString2(0,details.IndexOf(CRLF))
   
   
   Log("Name selected -" & cName & "-")
   cphone=details.SubString2(details.IndexOf(CRLF)+1,details.Length)
   
   
   Log("Phone number -" & cphone & "-")
   Activity.Title=cName & "-" & cphone 
   Return details
   
End Sub

Regards
Mark
 
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Halo Mark24at,

thanx for you help., but i can't sort listview!

B4X:
  'listOfContacts.Sort(True)

and this part of code made error .
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Sinan .. You cannot directly sort a ListView. In mark35at 's code listOfContacts was declared as a List object not ListView... Maybe populate a List withyour data, Sort ,then transfer to ListView. OR of your ListView is already populated then maybe ...

This is on presumption ListView is SingLine as in above example.

B4X:
Dim MyList As List
   MyList.Initialize
   
   For i = 0 To listOfContacts.Size -1
      MyList.Add(listOfContacts.GetItem(i))
   Next
   
   MyList.Sort(True)
   
   listOfContacts.Clear
   For i = 0 To MyList.Size -1
      listOfContacts.AddSingleLine(MyList.Get(i))
   Next

Cheers mj
 
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Hi mangojack,

this code not works

B4X:
MyList.Sort(True)

I get this error Messages in Log Windows
B4X:
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Sinan .. I do not get any error ?

B4X:
Sub Globals
      
   Dim listOfContacts As ListView
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   Activity.LoadLayout("main")

   listOfContacts.AddSingleLine("Paul")
   listOfContacts.AddSingleLine("John")
   listOfContacts.AddSingleLine("Abe")
   listOfContacts.AddSingleLine("Zino")
   
   Dim MyList As List
   MyList.Initialize
   
   For i = 0 To listOfContacts.Size -1
      MyList.Add(listOfContacts.GetItem(i))
   Next
      
   MyList.Sort(True)
   
   listOfContacts.Clear
   For i = 0 To MyList.Size -1
      listOfContacts.AddSingleLine(MyList.Get(i))
   Next
      
End Sub

Cheers mj
 
Upvote 0

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
ok..

why doesn't work at me

B4X:
Sub getcontacts
Dim Contact As Contact 
Dim Contacts2 As Contacts2
Dim liste As List
 
    liste.Initialize
    liste = Contacts2.GetAll(True,False)
   
    
    Dim MyList As List
    MyList.Initialize
    
    For i = 0 To liste.Size -1
        MyList.Add(liste.Get(i))
    Next
        
    MyList.Sort(True)
    
    ListView1.Clear
    For i = 0 To MyList.Size -1
       
        ListView1.AddTwoLines(MyList.Get(i),"")
       ListView1.TwoLinesLayout.ItemHeight = 55dip
         ListView1.ScrollingBackgroundColor = Colors.Transparent
       ListView1.FastScrollEnabled = True
         ListView1.TwoLinesLayout.Label.Typeface = Typeface.DEFAULT_BOLD
       ListView1.TwoLinesLayout.Label.TextSize = 20
       ListView1.TwoLinesLayout.Label.TextColor = Colors.Black
       ListView1.TwoLinesLayout.SecondLabel.TextSize = 13
       ListView1.TwoLinesLayout.SecondLabel.TextColor=Colors.DarkGray
       ListView1.Color=Colors.RGB(255,255,255)
       ListView1.TwoLinesLayout.Label.Gravity = Gravity.LEFT
   
    Next   

End Sub
:BangHead::BangHead::BangHead:
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
This is on presumption ListView is SingLine as in above example.

Is it possible your original List (liste) is NOT Single Line / Entry , If so you should be able to do ...
B4X:
liste.Sort(True)
... without transferring data to MyList first .

Sorry I cannot help you ...

Cheers mj
 
Last edited:
Upvote 0
Top