Android Question AS_SelectionList deselect item and select

stp

Active Member
Licensed User
Longtime User
Hi i use that command to deselect the first item in my list that has the id 0
deselect item:
AS_SelectionList1.DeselectItem2((0))
want now to send a comand to select that item. How can i do that ?
And how can i know if an item is checked ?
 
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime User
You can select item with SetSelections or with SetSelections2
B4X:
AS_SelectionList1.SetSelections(Array As Object(1,3))
B4X:
    Dim lst As List
    lst.Initialize
    lst.Add(1)
    lst.Add(3)
    AS_SelectionList1.SetSelections2(lst)

And how can i know if an item is checked ?
with GetSelections
B4X:
For Each Item As Object In AS_SelectionList1.GetSelections
        Log("Item selected: " & Item.Text)
    Next
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
Than you, i already use that loop in my code to log when an item or subitem is selected
select item or subitem:
Private Sub AS_SelectionList1_SelectionChanged
    
    For Each Item As Object In AS_SelectionList1.GetSelections
        If Item Is AS_SelectionList_Item Then
            Log("Item selected: " & Item.As(AS_SelectionList_Item).Text)
        Else
            Log("SubItem selected: " & Item.As(AS_SelectionList_SubItem).Text)
        End If
    Next

End Sub

but that sub acts only if you select an item. if you unselect it does nothing.
Is there no solution to ask for specific item or subitem to check if it is selected or not ?

and the first code:
B4X:
AS_SelectionList1.SetSelections(Array As Object(1,3))

    Dim lst As List
    lst.Initialize
    lst.Add(1)
    lst.Add(3)
    AS_SelectionList1.SetSelections2(lst)

if i use it to select an item, the specific item is selected but whole items in the list became unselected!!
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
but that sub acts only if you select an item. if you unselect it does nothing.
Try this the attached b4xlib, now the event is also triggered if you deselect via code.

if i use it to select an item, the specific item is selected but whole items in the list became unselected!!
That's exactly how it should be, that's how it was designed.
 

Attachments

  • AS_SelectionList.b4xlib
    12.1 KB · Views: 102
Upvote 0

stp

Active Member
Licensed User
Longtime User
take the example project, add a button then select an item you want to deselect and deselect it when you click the button
Why should i add a button? I want only to know which item are selected at any time. NOT on click. In your example there are items and subitems but it is not possible to know what is selected and what not. Only on Sub AS_SelectionList1_SelectionChanged i get a message if checked, if i unselect an item i get nothing.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Upvote 0

stp

Active Member
Licensed User
Longtime User
i call the GetSelections and i get an array

Array:
(ArrayList) [
[Icon=(B4XBitmap) Not initialized, IsInitialized=true, RootItem=
[Icon=(B4XBitmap) Not initialized, IsInitialized=true, Text=TikTok, Value=TikTok], Text=TikTok1, Value=TikTok1],
[Icon=(B4XBitmap) Not initialized, IsInitialized=true, RootItem=
[Icon=(B4XBitmap) Not initialized, IsInitialized=true, Text=TikTok, Value=TikTok], Text=TikTok2, Value=TikTok2],
[Icon=(B4XBitmap) Not initialized, IsInitialized=true, Text=News .. 1, Value=0],
[Icon=(B4XBitmap) Not initialized, IsInitialized=true, Text=News .. 2, Value=1]
]

I want to get the Text and the Value. How do i get that ?
 
Last edited:
Upvote 0

stp

Active Member
Licensed User
Longtime User
That is my code:
code:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Public  WobbleMenu1 As WobbleMenu
    Private AS_SelectionList1 As AS_SelectionList

End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("Categories")
    WobbleMenu1.SetTabTextIcon(1,"Newsroom",Chr(0xF009),Typeface.FONTAWESOME)
    WobbleMenu1.SetTabTextIcon(2,"Categories",Chr(0xF0CA),Typeface.FONTAWESOME)
    WobbleMenu1.SetTabTextIcon(3,"Bookmark",Chr(0xF0EC),Typeface.FONTAWESOME)
    WobbleMenu1.SetCurrentTab2(2, False)
    
    
    AS_SelectionList1.ThemeChangeTransition = AS_SelectionList1.ThemeChangeTransition_None
    AS_SelectionList1.Theme = AS_SelectionList1.Theme_Light
    AS_SelectionList1.SideGap = 5dip
    
'    AS_SelectionList1.MaxSelectionCount = 3
    
    'SubItems Example
    Dim RootItem As AS_SelectionList_Item = AS_SelectionList1.AddItem("Youtube", Null, "Youtube")
    AS_SelectionList1.AddSubItem(RootItem, "Youtube1", Null, "Youtube1")
    AS_SelectionList1.AddSubItem(RootItem, "Youtube2", Null, "Youtube2")
    AS_SelectionList1.AddSubItem(RootItem, "Youtube3", Null, "Youtube3")

    Dim RootItem2 As AS_SelectionList_Item = AS_SelectionList1.AddItem("TikTok", Null, "TikTok")
    AS_SelectionList1.AddSubItem(RootItem2, "TikTok1", Null, "TikTok1")
    AS_SelectionList1.AddSubItem(RootItem2, "TikTok2", Null, "TikTok2")
    AS_SelectionList1.AddSubItem(RootItem2, "TikTok3", Null, "TikTok3")
    AS_SelectionList1.AddSubItem(RootItem2, "TikTok4", Null, "TikTok4")

    Dim RootItem3 As AS_SelectionList_Item = AS_SelectionList1.AddItem("TweeterX", Null, "TweeterX")
    AS_SelectionList1.AddSubItem(RootItem3, "TweeterX1", Null, "TweeterX1")
    AS_SelectionList1.AddSubItem(RootItem3, "TweeterX2", Null, "TweeterX2")
    AS_SelectionList1.AddSubItem(RootItem3, "TweeterX3", Null, "TweeterX3")
    AS_SelectionList1.AddSubItem(RootItem3, "TweeterX4", Null, "TweeterX4")
    AS_SelectionList1.AddSubItem(RootItem3, "TweeterX5", Null, "TweeterX5")
    
    For i = 0 To 80 -1
        'AS_SelectionList1.AddItem("Test " & (i+1),AS_SelectionList1.FontToBitmap(Chr(0xF179),False,30,xui.Color_Black),i)
        AS_SelectionList1.AddItem("News .. " & (i+1),Null,i)
    Next

End Sub

Sub WobbleMenu1_Tab1Click
    B4XPages.MainPage.WobbleMenu1.SetCurrentTab(1)
    B4XPages.ShowPageAndRemovePreviousPages("MainPage")
End Sub

Sub WobbleMenu1_Tab2Click
End Sub

Sub WobbleMenu1_Tab3Click
    B4XPages.MainPage.Bookmark.WobbleMenu1.SetCurrentTab(3)
    B4XPages.ShowPageAndRemovePreviousPages("Bookmark")
End Sub


Private Sub AS_SelectionList1_SelectionChanged
    
    For Each Item As Object In AS_SelectionList1.GetSelections
        
        If Item Is AS_SelectionList_Item Then
            Log("Item selected: " & Item.As(AS_SelectionList_Item).Text)
            'Log("Item selected: " & Item.As(AS_SelectionList_Item))
            
        Else
            Log("SubItem selected: " & Item.As(AS_SelectionList_SubItem).Text)
            'Log("SubItem selected: " & Item.As(AS_SelectionList_SubItem))
        End If
            
        
    Next
    
    
    
    Log(" clicked")
    
    

End Sub



Private Sub Label1_Click
    'Log(AS_SelectionList1.GetSelections.Get(0))
    Log(AS_SelectionList1.GetSelections)
    Dim Res As Int
    'Dim res2 As Int
    Msgbox2Async("Select all categories and subcategories ?", "Attention", "Yes", "", "No",Null, False)
    Wait For Msgbox_Result (Res As Int)
    Log(Res)
    If Res =-1 Then
        'AS_SelectionList1.SetSelections((0))           
    End If
    
    
End Sub

Private Sub Label2_Click
    Dim Res As Int
    Msgbox2Async("Unselect all categories and subcategories ?", "Attention", "Yes", "", "No",Null, False)
    Wait For Msgbox_Result (Res As Int)
    Log(Res)
    If Res =-1 Then
        AS_SelectionList1.DeselectItem2("Youtube1")
        AS_SelectionList1.DeselectItem2("Youtube2")
        AS_SelectionList1.DeselectItem2("Youtube3")
        AS_SelectionList1.DeselectItem2("TikTok1")
        AS_SelectionList1.DeselectItem2("TikTok2")
        AS_SelectionList1.DeselectItem2("TikTok3")
        AS_SelectionList1.DeselectItem2("TikTok4")
        AS_SelectionList1.DeselectItem2("TweeterX1")
        AS_SelectionList1.DeselectItem2("TweeterX2")
        AS_SelectionList1.DeselectItem2("TweeterX3")
        AS_SelectionList1.DeselectItem2("TweeterX4")
        AS_SelectionList1.DeselectItem2("TweeterX5")
        
        AS_SelectionList1.DeselectItem2((0))
        
        For i = 0 To 80 -1
            'AS_SelectionList1.AddItem("Test " & (i+1),AS_SelectionList1.FontToBitmap(Chr(0xF179),False,30,xui.Color_Black),i)
            'AS_SelectionList1.DeselectItem("News" & (i+1))
        
            AS_SelectionList1.DeselectItem2((i+1))
        Next
        
        
    End If
    
End Sub
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
I want to get the Text and the Value. How do i get that ?
You've already got that right in your code:
Log("Item selected: " & Item.As(AS_SelectionList_Item).Text)
where is the problem?

Maybe it is easier to tell me what your goal is, maybe then I will understand your code with the 20 deslections, which are not necessary because there is a ClearSelections function. Then we both save time and effort.
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
ok solved i created a button and made some test..
code:
Private Sub Label1_Click
    'Log(AS_SelectionList1.GetSelections.Get(0))
    'size gives us the num of selected items
    Log(AS_SelectionList1.GetSelections.Size)
    For n=0 To AS_SelectionList1.GetSelections.Size-1
        Log(AS_SelectionList1.GetSelections.Get(n))

       
    Next
    Log("==========================================================")
    For Each Item As Object In AS_SelectionList1.GetSelections
       
        If Item Is AS_SelectionList_Item Then
            Log("Item selected: " & Item.As(AS_SelectionList_Item).Text)
            Log("Item selected: " & Item.As(AS_SelectionList_Item).Value)
           
        Else
            Log("SubItem selected: " & Item.As(AS_SelectionList_SubItem).Text)
            Log("SubItem selected: " & Item.As(AS_SelectionList_SubItem).value)
        End If
           
       
    Next
   
    Dim Res As Int
    'Dim res2 As Int
    Msgbox2Async("Select all categories and subcategories ?", "Attention", "Yes", "", "No",Null, False)
    Wait For Msgbox_Result (Res As Int)
    Log(Res)
    If Res =-1 Then
        'AS_SelectionList1.SetSelections((0))          
    End If
   
   
End Sub

now i have the values

That is not what i want but i will create an array and store that values, so i can compare at any time.
 
Upvote 0

stp

Active Member
Licensed User
Longtime User
Ok. I have a list with some subitems and items.
There are 2 buttons on the top. Select all and deselect all. That means that has to act also in the sublist.
That selections have to be saved in a text file, so after opening the app the selections must be there.
I will attach whole project with my debuging prints. So you can follow my progress.
Thank you
 

Attachments

  • mytest-001.zip
    24 KB · Views: 90
Upvote 0

stp

Active Member
Licensed User
Longtime User
i created a button to select all 80 items my code is
code:
Private Sub Button2_Click
    'AS_SelectionList1.SetSelections
    Dim lst As List
    lst.Initialize
    'lst.Add(1)
    'lst.Add(3)
    For n=0 To 79
        lst.Add(n)
    Next
   
    AS_SelectionList1.SetSelections2(lst)
End Sub

that code selects all 80 items.. how can i select also all sub items ?
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
There are 2 buttons on the top. Select all and deselect all. That means that has to act also in the sublist.
Okay, you can simply deselect everything with AS_SelectionList1.ClearSelections

and select everything with the new AS_SelectionList1.SelectAll command.

That selections have to be saved in a text file, so after opening the app the selections must be there.
Saving:
B4X:
    xui.SetDataFolder("example")
    Dim lst As List
    lst.Initialize
    
    For Each Item As Object In AS_SelectionList1.GetSelections
        
        If Item Is AS_SelectionList_Item Then
            lst.Add(Item.As(AS_SelectionList_Item).Value)
        Else
            lst.Add(Item.As(AS_SelectionList_SubItem).Value)
        End If
        
    Next
    
    File.WriteList(xui.DefaultFolder,"Selections.txt",lst)
Loading:
B4X:
    Dim lst As List
    lst.Initialize
    
    For Each Value As Object In File.ReadList(xui.DefaultFolder,"Selections.txt")
        
        lst.Add(Value)
        
    Next
    
    AS_SelectionList1.SetSelections2(lst)
 

Attachments

  • AS_SelectionList.b4xlib
    12.2 KB · Views: 87
Upvote 0
Top