Android Question [SOLVED] - Changing the text property of a label view in an xCLV at a specific index

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I built a CLV using this coding:

B4X:
    Activity.LoadLayout("SongList") ' This layout also contains a custom list view.

    CSelections.Initialize(clvSongList)
    CSelections.Mode = CSelections.MODE_SINGLE_ITEM_PERMANENT

    ' Make panel slide down out of site.
    '-----------------------------------
    PanelMenu.SetLayoutAnimated(300, 0, Activity.Height, Activity.Width, _
        Activity.Height)
        
    ' Add 10 items to the list view.
    '-------------------------------
    For intIndex = 0 To 9
        
        ' typValues will be used to store values into the Type elements for the list views.
        '----------------------------------------------------------------------------------
        Dim typValues As typListItemValues

        ' Adding items to the list view requires a Panel created in code.
        '----------------------------------------------------------------
        Dim p As B4XView = xui.CreatePanel("")

        ' This is the size for the panel and it will be animated.
        '--------------------------------------------------------
        p.SetLayoutAnimated(100, 0, 0, PanelSongList.Width, 200dip)
        
        p.LoadLayout("SongListCard")

        ' Populate the views with values from the database.
        '--------------------------------------------------       
        Select intIndex
            Case 0
                LabelSongTitle.Text = kvs.Get("Song1Title")
                LabelSchedule.Text = kvs.Get("Song1Schedule")
                
                If kvs.Get("Song1ScheduleIsActivated") Then
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-on-icon.png")
                Else
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-off-icon.png")
                End If
                
                typValues.strSomeMoreText = "One"
                
            Case 1
                LabelSongTitle.Text = kvs.Get("Song2Title")
                LabelSchedule.Text = kvs.Get("Song2Schedule")

                If kvs.Get("Song2ScheduleIsActivated") Then
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-on-icon.png")
                Else
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-off-icon.png")
                End If
                
                typValues.strSomeMoreText = "Two"

            Case 2
                LabelSongTitle.Text = kvs.Get("Song3Title")
                LabelSchedule.Text = kvs.Get("Song3Schedule")

                If kvs.Get("Song3ScheduleIsActivated") Then
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-on-icon.png")
                Else
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-off-icon.png")
                End If
                
                typValues.strSomeMoreText = "Three"
        End Select

        typValues.LabelSongTitle = LabelSongTitle.Text
        ImageViewPlayPauseSong.Tag = intIndex
        
        ' This is where the item row is added to the list view.
        ' Each list item has a set of Type structure elements associated with it.
        '------------------------------------------------------------------------
        clvSongList.Add(p,typValues)
    Next

One of the views in the CLV item is called LabelSchedule. Elsewhere in my coding, the label will need to be changed to reflect a change in the schedule. I would like to change the text property of that label which happens to be at index number 3. For example I would like to change the text to "This is the new schedule...". I only want to change the text at that index. Can this be done without rebuilding the CLV?

The file attachment shows LabelSchedule in the Designer.

Thanks.

Screenshot 2020-11-16 at 20.31.47.png
 

mangojack

Expert
Licensed User
Longtime User
Elsewhere in my coding, the label will need to be changed ...


Knowing the CLV row index you want to change, it would just be a matter of ...
B4X:
Dim pnl As B4XView = clvSongList.GetPanel(index)
pnl.GetView(3).Text = "This is the new schedule..."  'View/Index 3 = LabelSchedule


If this needed change is a result of clicking another Item/row View then you would have to get the index first with ...
B4X:
Dim index As Int = clvSongList.GetItemFromView(Sender)
   '.........
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Knowing the CLV row index you want to change, it would just be a matter of ...
B4X:
Dim pnl As B4XView = clvSongList.GetPanel(index)
pnl.GetView(3).Text = "This is the new schedule..."  'View/Index 3 = LabelSchedule


If this needed change is a result of clicking another Item/row View then you would have to get the index first with ...
B4X:
Dim index As Int = clvSongList.GetItemFromView(Sender)
   '.........
Thanks for the reply. I tried it, but the logs tell me the view is not initialised. I thought that having the views in the layout like I have it in the designer makes all of the views initialised. How do I initialise it?

B4X:
main_imageviewplaypausesong_click (java line: 688)
java.lang.RuntimeException: Object should first be initialized (View).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
    at anywheresoftware.b4a.objects.B4XViewWrapper.GetView(B4XViewWrapper.java:317)
    at b4a.natures.song.main._imageviewplaypausesong_click(main.java:688)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7869)
    at android.view.View.performClickInternal(View.java:7838)
    at android.view.View.access$3600(View.java:886)
    at android.view.View$PerformClick.run(View.java:29362)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:8125)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

I tried a log statement and discovered only the panel at view index 0 from the designer was initialised. All the other views such as the label at 3 were not initialised.

B4X:
Log("Is it initialised? " & pnl.GetView(0).IsInitialized)

Thanks in advance.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
B4X:
pnl.GetView(3).Text = "This is the new schedule..."
should be since you have a child panel on the base panel:
B4X:
pnl.GetView(0).GetView(3).Text = "This is the new schedule..."
Nailed it! ?

Thanks so much. That was tricky to get my head around. I would have never figured that one out. With your help now I know how to do it because all my xCLV's will have panels like that to simulate cards. You were very helpful.
 
Upvote 0
Top