CustomListView - A flexible list based on ScrollView

Status
Not open for further replies.

jtare

Active Member
Licensed User
Longtime User
Hi,

How to change the background colour of an individual item before .AddTextItem ?

Paul

Maybe you could try changing in the CustomListView Class the following:
B4X:
'Adds a text item. The item height will be adjusted based on the text.
Public Sub AddTextItem(Text As String, Value As Object, BackgroundColor As Int)
    InsertAtTextItem(items.Size, Text, Value, BackgroundColor)
End Sub

'Inserts a text item at the specified index.
Public Sub InsertAtTextItem(Index As Int, Text As String, Value As Object, BackgroundColor As Int)
    Dim pnl As Panel
    pnl.Initialize("")
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    pnl.AddView(lbl, 5dip, 2dip, sv.Width - 5dip, 20dip)
    lbl.Text = Text
    lbl.TextSize = DefaultTextSize
    lbl.TextColor = DefaultTextColor
'    If DefaultTextBackground <> Null Then
        'pnl.Background = DefaultTextBackground
    'Else
        pnl.Color = BackgroundColor
    'End If
    Dim minHeight As Int
    minHeight = su.MeasureMultilineTextHeight(lbl, Text)
    lbl.Height = Max(50dip, minHeight)
    InsertAt(Index, pnl, lbl.Height + 2dip, Value)
End Sub

Example:
B4X:
    For i = 1 To 10
        If i = 10 Then
        clv2.AddTextItem("Text"&i,"",Colors.Blue)
        Else
        clv2.AddTextItem("Text"&i,"",Colors.Red)
        End If   
    Next
 

LucaMs

Expert
Licensed User
Longtime User
How to change the background colour of an individual item before .AddTextItem ?
You can change the backcolor just after adding the new item:
B4X:
clv1.GetPanel(clv1.GetSize -1).Color = Colors.Blue

Otherwise you need to change two routines, as @jtare suggested. I would do it so:
B4X:
'Adds a text item. The item height will be adjusted based on the text.
' Pass BackgroundColor = -1 to use default background.
Public Sub AddTextItem(Text As String, BackgroundColor As Int, Value As Object)
    InsertAtTextItem(items.Size, Text, BackgroundColor, Value)
End Sub

'Inserts a text item at the specified index.
' Pass BackgroundColor = -1 to use default background.
Public Sub InsertAtTextItem(Index As Int, Text As String, BackgroundColor As Int, Value As Object)
    Dim pnl As Panel
    pnl.Initialize("")
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    pnl.AddView(lbl, 5dip, 2dip, sv.Width - 5dip, 20dip)
    lbl.Text = Text
    lbl.TextSize = DefaultTextSize
    lbl.TextColor = DefaultTextColor
    If BackgroundColor <> - 1 Then
        pnl.Color = BackgroundColor
    Else
        If DefaultTextBackground <> Null Then
            pnl.Background = DefaultTextBackground
        Else
            pnl.Color = DefaultTextBackgroundColor
        End If
    End If
    Dim minHeight As Int
    minHeight = su.MeasureMultilineTextHeight(lbl, Text)
    lbl.Height = Max(50dip, minHeight)
    InsertAt(Index, pnl, lbl.Height + 2dip, Value)
End Sub
 

vbmundo

Well-Known Member
Licensed User
Hi,

Can I make special ListView with your CustomListView ?

I mean, adding a menu in every element ..

I wish to create this.

ControlNuevo1.jpg

ControlNuevo2.jpg


Regards
 

frenkipi

Member
Licensed User
Longtime User
I there a way to add another value like...

B4X:
clv2.Add(CreateListItem($"Item #${i}"$, clv2.AsView.Width, 70dip), 70dip, $"Item #${i}"$, "another value"&i)

I was trying with modifying customListView.... but with no luck.

Thank you for your help!
 

vbmundo

Well-Known Member
Licensed User
HI,

I'm using this great class into my new brand APP.

I show a panel with labels, and a Swich view.

But how can I interact to those elements ? I mean, when the user turn OFF the Switch, I need to set ENABLED=FALSE in the rest of labels and views.

But the

B4X:
Sub OnOff_CheckedChange(Checked As Boolean)
    Msgbox("Cambio de Status","Cambio")
End Sub

Don't return the CLV Item, and all Swich will raise the same event. CLV don't throw the INDEX item on every element event.

What Can I do ?

Regards
 

vbmundo

Well-Known Member
Licensed User
Look at Erel's example on Post 1; it shows how to get the view from the each CLV base panel

Thanks !!

But when I run this feature in my App I have this error

B4X:
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:com.alarma.clock
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (alarma) Create **
Error occurred on line: 216 (CustomListView)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:258)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:118)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
    at com.alarma.clock.customlistview._getitemfromview(customlistview.java:84)
    at com.alarma.clock.main._onoff_checkedchange(main.java:827)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.objects.CompoundButtonWrapper$1.onCheckedChanged(CompoundButtonWrapper.java:44)
    at android.widget.CompoundButton.setChecked(CompoundButton.java:154)
    at android.support.v7.widget.SwitchCompat.setChecked(SwitchCompat.java:1066)
    at de.amberhome.objects.appcompat.ACSwitchCompatWrapper.DesignerCreateView(ACSwitchCompatWrapper.java:63)
    at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:70)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:158)
    at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:897)
    at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:831)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at com.alarma.clock.alarma.onCreate(alarma.java:54)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2907)
    at android.app.ActivityThread.access$2200(ActivityThread.java:165)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5593)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **

This is the CODE that fail

B4X:
'Returns the index of the item that holds the given view.
Public Sub GetItemFromView(v As View) As Int
    Dim parent = v, current As Object
    Do While (parent Is Panel) = False Or sv.Panel <> parent
        current = parent
        Dim jo As JavaObject = current
        parent = jo.RunMethod("getParent", Null)
    Loop
    v = current
    Return v.Tag
End Sub

This CODE is call from this other

B4X:
Sub OnOff_CheckedChange(Checked As Boolean)
    'If CargaInicial Then Return
   
    Dim index As Int= ClvAlarmas.GetItemFromView(Sender)
    Dim pnl As Panel = ClvAlarmas.GetPanel(index)
    Dim chk As ACSwitch = pnl.GetView(3)
    Msgbox($"Item value: ${ClvAlarmas.GetValue(index)}
Check value: ${chk.Checked}"$, "")

End Sub

Maybe, this CustomListView don't allow an AcSwitch
 
Status
Not open for further replies.
Top