Android Question Problem with CustomListView control

vbmundo

Well-Known Member
Licensed User
Hi,

In this POST
https://www.b4x.com/android/forum/t...w-to-get-view-index-values.69907/#post-452411

I comment that I have a Layout with panels, Labels, Imageview and one ACSwich

Erel and DonManfred give me some possible solutions, but I can't solve this issue.

In my CreateListItem I add some new parameters and my code is that

B4X:
Sub CreateListItem(Hora As String, Dias As String, NombreAlarma As String, Activa As Boolean, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    Activity.AddView(p, 0, 0, Width, Height)
    p.LoadLayout("ItemAlarma")
    p.RemoveView
    lblHora.Text=Hora
    lblDias.Text=Dias
    lblNombre.Text=NombreAlarma
    OnOff.Checked=Activa
    lblHora.Enabled=Activa
    lblDias.Enabled=Activa
    lblNombre.Enabled=Activa
    If Activa Then
         IconoON.Visible=True
         IconoOFF.Visible=False
    Else
        IconoON.Visible=False
        IconoOFF.Visible=True 
    End If
    Return p
End Sub

The Item is displayed correctly

The problem is when I want to change the ACSwitch state, turn OFF or ON

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

End Sub

I receive this error

B4X:
Error occurred on line: 132 (Main)
Message longer than Log limit (4000). Message was truncated.
java.lang.RuntimeException: Object should first be initialized (View).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    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.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 android.support.v7.widget.SwitchCompat.stopDrag(SwitchCompat.java:1007)
    at android.support.v7.widget.SwitchCompat.onTouchEvent(SwitchCompat.java:957)
    at android.view.View.dispatchTouchEvent(View.java:8505)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2390)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1734)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2833)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2351)
    at android.view.View.dispatchPointerEvent(View.java:8700)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4415)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4281)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3779)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3832)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3798)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3915)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3806)
    at android.v

This is the line with error

chk = pnl.GetView(5)

I has changed the parameter, from 0 to 10 and allways have error.

This is my Tree View (see attached file)
errorCustomListView.jpg


I'm hold in this problem

Thanks
 

rscheel

Well-Known Member
Licensed User
Longtime User
Debes inicializar el panel

You must initialize the panel

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

End Sub
 
Upvote 0

vbmundo

Well-Known Member
Licensed User
Debes inicializar el panel

You must initialize the panel

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

End Sub

Thanks

But still doesn't work

Look, I modify the code

B4X:
Sub OnOff_CheckedChange(Checked As Boolean)
    If CargaInicial Then Return
    Dim Indice As Int=0 
    Dim index As Int= ClvAlarmas.GetItemFromView(Sender)
    Dim pnl As Panel
    pnl.Initialize("pnl") 
    pnl = ClvAlarmas.GetPanel(index)
   
    Dim chk As ACSwitch
    chk.Initialize("chk")
    For Each Vista As View In pnl.GetAllViewsRecursive
        If Vista Is ACSwitch Then
            Exit
        End If
        Indice=Indice+1
    Next
    Msgbox(Indice,"Indice")
    chk = pnl.GetView(Indice)
End Sub

The Index of the ACSwitch is "4"

Chk is initialized in this line

chk.Initialize("chk")

But I still receive this error

B4X:
Error occurred on line: 142 (Main)
Message longer than Log limit (4000). Message was truncated.
java.lang.RuntimeException: Object should first be initialized (View).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    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.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:1061)
    at android.support.v7.widget.SwitchCompat.stopDrag(SwitchCompat.java:1002)
    at android.support.v7.widget.SwitchCompat.onTouchEvent(SwitchCompat.java:952)
    at android.view.View.dispatchTouchEvent(View.java:8505)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2390)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1734)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2833)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2351)
    at android.view.View.dispatchPointerEvent(View.java:8700)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4415)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4281)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3779)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3832)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3798)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3915)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3806)
    at android.v
Message longer than Log limit (4000). Message was truncated.
** Activity (main) Pause, UserClosed = true **

I use CustomListView in other APP without problem... the unique difference is that in this APP I use an ACSwitch

I don't know what to do.

Thanks
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Sub OnOff_CheckedChange(Checked As Boolean)
If CargaInicial Then Return
Dim index As Int= ClvAlarmas.GetItemFromView(Sender)
Dim pnl As Panel
pnl = ClvAlarmas.GetPanel(index)
Dim chk As ACSwitch

chk = pnl.GetView(
5)
Msgbox($"Item value: ${ClvAlarmas.GetValue(index)}
Check value: ${
chk.Checked}"$, "")

End Sub

In your sub OnOff_CheckedChange the Sender will be the ACSwitch, so you can write directly:
Dim chk As ACSwitch = Sender


But:
The problem is when I want to change the ACSwitch state, turn OFF or ON
I don't understand; the event routine OnOff_CheckedChange raises when you change the ACSwtich state: what do you want to achieve?
If you want then set:
chk.Checked = True
or
chk.Checked = Not(chk.Checked)

your code will loop.
 
Upvote 0

vbmundo

Well-Known Member
Licensed User
The think is..

In need to capture when the user turn On or Off the Switch , but I need to Know in what Item.. because, I need to turn ENABLED=FALSE o TRUE to the rest of views depends of the Item

Look this happend with your previous code

CustomListView2.png


Only 1 View...

As I tell you, I need to capture the INDEX... I have 1 ImageView, 2 Labels to Turn ENABLED True o False.

Regards
 
Upvote 0

vbmundo

Well-Known Member
Licensed User
Pass the panel (item) to this routine:
B4X:
Private Sub EnableView(pView As View, pEnabled As Boolean)
    pView.Enabled = pEnabled
    If pView Is Panel Then
        Dim pnl As Panel = pView
        For Each v As View In pnl.GetAllViewsRecursive
            EnableView(v, pEnabled)
        Next
    End If
End Sub

Hi but

My code now is that

B4X:
Sub OnOff_CheckedChange(Checked As Boolean)
    If CargaInicial Then Return
    Dim accb As ACSwitch = Sender
    Dim index As Int = ClvAlarmas.GetItemFromView(accb)
    
End Sub

How can I capture the PANEL item with this code ?

Regards
 
Upvote 0

vbmundo

Well-Known Member
Licensed User
Dim pnlItem As Panel = ClvAlarmas.GetPanel(Index)

Perfect !! Thanks

Just a single more question

Look this is my Activity

Alarmas.png


Now (for your help) I'm available to detect the ITEM Index and I can modify all views to Enabled or not Enabled.

But if you look, I have an Image or ICON that represent the Alarm State... Green is ON, and Red is OFF.

When I read all views in the Panel, I could not get the name of control I'm reading, because , if the OnOff ACSwitch is CHECKED then I need to find the ImageView named "IconoON" and set VISIBLE=TRUE, and "IconoOFF" set VISIBLE=FALSE

I can't find the NAME attribute in any view.

Regards
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I can't find the NAME attribute in any view.
This is an "old" question; you can find many posts about it: Views have no name!

However:
1) you know the index of the imageviews in the item panel, then you can get them and set...
2) usually the Tag property is used to "get the right view"
3) you could use a single imageview and set its bitmap (declare two bitmaps and load them in Activity_Creae) as needs (in the CheckedChange event).
 
Upvote 0

vbmundo

Well-Known Member
Licensed User
This is an "old" question; you can find many posts about it: Views have no name!

However:
1) you know the index of the imageviews in the item panel, then you can get them and set...
2) usually the Tag property is used to "get the right view"
3) you could use a single imageview and set its bitmap (declare two bitmaps and load them in Activity_Creae) as needs.

Yes, I think that your 3rd recomendation is the best way

Thanks
 
Upvote 0
Top