Android Question how create and use a class ? How to about variables ?

fgh3966

Active Member
Licensed User
Hello everybody
Hello, I am trying to create a class based on an example from the B4X manual.
I manage to create the decodleds class but I have difficulties to initialize the class or the qrt2 variable, the main program does not see qrt2 of decodleds. Can we initialize variables in the class, for example the imageview?
The goal of the program is to change the value of qrt2 (click on 1 button for example) then the decodleds class retrieves qrt2 and displays the 4 good images representing a quartet of leds according to the value of qrt2.
I don't understand why B4A is asking me for an "array" ?

Is there a simple example for creating a class?

Here is the source code

Thank you beforehand.

classe decodleds:
Sub Class_Globals
    Private led5,led6,led7,led8 As ImageView
    Private qrt2 As Byte 
End Sub

'Sub Process_Globals
'    'These global variables will be declared once when the application starts.
'    'These variables can be accessed from all modules.
'
'    Public QRT2 As Byte
' 
'
'End Sub

'Initializes the object. You can add parameters To this method If needed.
Sub Initialize
    qrt2 = 0
  
End Sub

public Sub quartet2 As Byte
    'Dim ImageWiew1, ImageWiev2, ImageWiev3, ImageWiev4)
    'Dim led5,led6,led7,led8 As ImageView

    If qrt2 = 0 Then
        '******************************** 0 *************************************
        led5.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        led6.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        led7.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        led8.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
    End If
  
    If qrt2 = 2 Then
        '******************************** 0 *************************************
        led5.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        led6.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        led7.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        led8.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
    End If
  
    If qrt2 = 4 Then
        '******************************** 0 *************************************
        led5.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        led6.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        led7.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        led8.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
    End If

End Sub


main program:
#Region  Project Attributes
    #ApplicationLabel: nixie counter
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
#End Region

'Activity module
Sub Process_Globals
    Public QRT1 As Byte
'    Public QRT2 As Byte
    Public BV1 As Byte 
    Public BV2 As Byte 
End Sub

Sub Globals
    Private pnl1 As Panel
    Private cvsGraph As Canvas
    Private Label1 As Label
    Private ImageView1 As ImageView 
    Private ImageView2 As ImageView
    Private ImageView3 As ImageView
    Private ImageView4 As ImageView
    Private Lbll6 As Label
  
    End Sub

Sub Activity_Create(FirstTime As Boolean)
      
    Activity.LoadLayout("main")        ' load the layout
    cvsGraph.Initialize(pnl1)    ' initialize the Canvas for the panel
  
    Dim Deco As decodleds
    'Deco.initialize(0, 0)
  
    pnl1.Invalidate
    'set zeros     
End Sub

Sub quartet1
  
    If QRT1 = 3 Then
        '******************************** 0 *************************************
        ImageView1.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView2.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView3.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        ImageView4.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
    End If
      
    If QRT1 = 1 Then
        '******************************** 1 *************************************
        ImageView1.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView2.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView3.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView4.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
    End If
      
    If QRT1 = 2 Then
        '******************************** 2 *************************************
        ImageView1.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView2.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView3.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        ImageView4.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
    End If


End Sub

Sub btunit_Click (Deco As decodleds) As Byte
  
    BV1 = BV1 + 1
    QRT1=Bit.And(0x0F,BV1)       
    quartet1
    BV2 = BV1/16
    qrt2 = Bit.And(0x0F, BV2)
    Deco.quartet2(qrt2)
'    Deco.quartet2(QRT2)
'    D.initialise (QRT2)
  
    Lbll6.Text = BV1 
    Label1.Text = "+++" 
End Sub

Sub btdiz_Click 
    BV1 = 0
    QRT1 = 0
'    QRT2 = 0
    quartet1 

    Lbll6.Text = BV1 
    Label1.Text = "ZZERO"     
End Sub

Sub btcent_Click 
    BV1 = BV1 - 1
    QRT1=Bit.And(0x0F,BV1)
    quartet1
    BV2 = BV1/16
'    QRT2 = Bit.And(0x0F, BV2)

    Label1.Text = "---" 
    If BV1 > 15 Then
        Label1.Text = "33ces"
    End If
    Lbll6.Text = BV1
  
End Sub

Sub Activity_Resume
  
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

DonManfred

Expert
Licensed User
Longtime User
The goal of the program is to change the value of qrt2
-You can make the variable Public instead of Private.

- You also can create a Getter and/or Setter to get or set the Variable without setting it to Public.

B4X:
' Getter
sub getqrt2 as Byte
    return qrt2
end sub
' Setter
sub setqrt2(value As Byte)
    qrt2 = value
end sub
 
Upvote 0

fgh3966

Active Member
Licensed User
i write

into decodleds classe:
Sub quartet2 (qrt2 As Byte)

But into main code qrt2 are in red color, so when i click to btnunit to application, my phone say message : Sub btnunit_click signature does not match expected signature.
Continue ? no or yes

sub (btnunit_click) main code:
Sub btunit_Click (Deco As decodleds) As Byte
   
    BV1 = BV1 + 1
    QRT1=Bit.And(0x0F,BV1)        
    quartet1
    BV2 = BV1/16
    qrt2 = Bit.And(0x0F, BV2)
    Deco.quartet2(qrt2)
'    Deco.quartet2(QRT2)
'    D.initialise (QRT2)
   
    Lbll6.Text = BV1  
    Label1.Text = "+++"  
End Sub
 
Upvote 0

teddybear

Well-Known Member
Licensed User
You phone would like to find Sub btnunit_click not Sub btunit_Click (Deco As decodleds) As Byte
You should know what btn_click event is.
 
Upvote 0

fgh3966

Active Member
Licensed User
the btn_click event is a button being clicked, which changes the value of a variable.
I would like qrt2 to be this variable.
Can we call decodleds in the sub: btunit_Click ?
So how do you call a function in a class? Because it doesn't work for me

I have read and tried to understand your link earlier and it is not easy to understand.
The examples are different, seem complex to me and I don't quite see how to call the class and use it.
Sorry.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
@fgh3966
I am not in front of my PC, and from mobile it's not easy to understand. ?
I think you are just getting confused about the flow between Main and the Class to pass parameters.
I see qrt1 in Main and qrt2 in Class.
Then I see qrt2 commented in Main.
What should exactly been passed from Main to Class?
And what the Class should give back as result to Main?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
1st, you should understand what class is
2nd, if btunit is a button, it just is Sub btunit_click, you can't change its signature to btunit_Click (Deco As decodleds) As Byte
 
Upvote 0

fgh3966

Active Member
Licensed User
Teddybear say : 1st, you should understand what class is.

If everyone give me a very-very simple example, It will help me to understand what it class and how it works.
Also how to recognize the class in the main file?
If possible by noting the headings, otherwise I would not understand.
Is it possible to have a simple and complete example of the main file and the class? It should not exceed 30 lines in all.
Also in my decodleds class I can't initialize the bitmaps.
It's really complicated

I sent you the sources, if someone could clearly point out the errors to me?

Thanks in advance

Regards
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Try this as a begin.
 

Attachments

  • DecoLeds.zip
    73.3 KB · Views: 64
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find a small project showing two possibilities.
The first with a standard class, the LEDs are defined in the Main module and transmitted to the class in the Initialize routine.
And the second as a CustomView, the Leds are initialized in the CustomView module.
 

Attachments

  • TestClass.zip
    17 KB · Views: 91
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…