listview SetSelection highlight

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,

I am using a single line listview to show a list of files. I would like to use SetSelection to highlight the chosen file.

How do you set a particular item to be the highlighted item? I want to do this as a response to an item click and also when populating the list.

I have tried many different things. Below is my latest attempt.

B4X:
  j = -1
  For i=0 To WFList.Size-1
    s = WFList.Get(i)
    lvWFile.AddSingleLine(s)
    If (s.CompareTo(WFName) = 0) Then j = i
  Next
  
  If (j >= 0) Then lvWFile.SetSelection(j)

Thanks,
Barry.
 

specci48

Well-Known Member
Licensed User
Longtime User
As Erel mentioned in a different thread, the ListView doesn't really have a "selected item".
As a alternative you can use a scrollview, which means some additional overhead but as a scrollview contains other views you will get full access to every single item to change colors, text, etc.


specci48
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User

Thanks. I think that will work out very well.

Barry.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Listview Item highlight

Hello, if i undersand correctly, does this mean that there's no way to highlight a single item in a listview, i've seen it done in other apps, can this be done with Basic4Android?

thanks,
Walter
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
Thanks. I think that will work out very well.

Barry.
Hi,

I know that it was a long time but just sharing....

You can simulate item selected with listview using "AddTwoLinesAndBitmap" method. You put a icon X at item select and a default icon (like a transparent one) to others rows.
B4X:
Sub fillLstFiles (itemSelected As Int)

    Dim programTemp As Program
    programTemp = Codes.GetProgram(programInExecution.ID,False)
    lstFiles.Clear 'clear listview
    For i = 0 To programTemp.Num_Files - 1
        If i = itemSelected Then
            lstFiles.AddTwoLinesAndBitmap(programTemp.Current_File,"",LoadBitmap(File.DirAssets,"1398500985_clean.png"))
        Else
            lstFiles.AddTwoLinesAndBitmap(programTemp.Current_File,"",LoadBitmap(File.DirAssets,"1398501184_checkbox-unchecked.png"))
        End If
        programTemp.moveToNextFile
    Next
       
End Sub

Sub lstFiles_ItemClick (Position As Int, Value As Object)
   fillLstFiles(Position)
End Sub

Regards,
Alexandre Riani
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…