Android Question Regex problem

stari

Active Member
Licensed User
Longtime User
I try App from @LucaMs.
I put the code in B4XPage ( @Erel example ThreePagesExample).
The code from @LucaMs is working fine, but in B4XPages i get error:
B4A Version: 10.2
Java Version: 11
Parsing code. (0.18s)
Building folders structure. (0.05s)
Running custom action. (0.05s)
Compiling code. (0.73s)
Compiling layouts code. (0.01s)
Organizing libraries. (0.00s)
(AndroidX SDK)
Generating R file. (0.00s)
Compiling debugger engine code. (1.26s)
Compiling generated Java code. Error
B4A line: 83
card_name = Regex.Split(\
src\b4a\example\ccrd.java:141: error: cannot find symbol
_card_name = parent.__c.Regex.Split("_",BA.ObjectToString(_tappedcard._gettag()))[(int) (0)].replace("_"," ").replace(".png","");
^
symbol: method _gettag()
location: variable _tappedcard of type b4ximageview

And the code:
B4A line 83: here is line 9

B4X:
Sub B4XImgViewCard_OneClick

    Dim  slowd As Int = 50
    Dim card_name As String
      
    If Not(Angle = 0) Then Return 'Wait for card angle to be 0 before spinning the nect card
    Dim TappedCard As B4XImageView = Sender 'Create a sender to capture B4XImageView click
      
    card_name = Regex.Split("_", TappedCard.Tag)(0).Replace("_", " ").Replace(".png", "") 
    LogColor(card_name,Colors.Blue)
  
    Dim CardRotation As Int = Regex.Split("~", TappedCard.Tag)(1) 'Read card position
    CardFace = xui.LoadBitmapResize(File.DirAssets, Regex.Split("~", TappedCard.Tag)(0), CardSize, CardSize, True) 'Load the card face image
    DisableCards(True) 'Disable cards

    If 180 = MapRotation.Get(CardRotation) Then Angle = 180  'Check last tapped card rotation position against the current angle and set the rotation angle
              
    For i = 0 To 180
        #If debug
        slowd= 1
        #end if     
        Sleep(slowd)
          
        Angle = Angle + AddAngle 'Set the angle
        B4XImgViewCard(CardRotation).Rotate(0, Angle + 180, 0) 'Rotate the card to the newly set angle

        Select Angle
            Case 90 'At 90°
                B4XImgViewCard(CardRotation).B4XBitmap = CardFace 'Set card face image
            Case 180 'At 180°                                        'če to izpustim, se karta zavrti za 360
                Angle = 0
                MapRotation.Put(CardRotation, 180) 'Set card rotation to angle
                DisableCards(False) 'Enable cards
                Log("End of Card_ON_Click - 180")
                Return
            Case 270 'At 270°
                B4XImgViewCard(CardRotation).B4XBitmap = CardBack 'Set card back image
              
            Case 360 'At 360°
                Angle = 0
                MapRotation.Put(CardRotation, 0) 'Set card rotation to 0
                DisableCards(False) 'Enable cards
                Log("End of Card_ON_Click - 360")
                Return
        End Select
        Sleep(0) 
    Next
  
End Sub

I have no idea, what i make wrong.
 

kisoft

Well-Known Member
Licensed User
Longtime User
I am not convinced that the code syntax is correct. For example this:
B4X:
card_name = Regex.Split (" _", TappedCard.Tag) (0) .Replace ("_", "") .Replace (". Png", "")

Here's an example of code that works, although the separator is a vertical bar that has a special meaning ...
B4X:
Sub Activity_Click

    Dim str As String = "abc | def | efg"
    Dim x () As String = Regex.Split ("\ |", str)
 
Last edited:
Upvote 0

stari

Active Member
Licensed User
Longtime User
card_name should be a String array. Is there a type warning in the Logs pane?
Thks @agraham

No, no type warning in the Logs pane.

If I omit line 83, then the same error is here:
B4X:
 Dim CardRotation As Int = Regex.Split("~", TappedCard.Tag)(1) 'Read card position

In the original program (@LucaMs ), without B4XPages, it works without problems .
 
Upvote 0

stari

Active Member
Licensed User
Longtime User
I am not convinced that the code syntax is correct. For example this:
B4X:
card_name = Regex.Split (" _", TappedCard.Tag) (0) .Replace ("_", "") .Replace (". Png", "")

Here's an example of code that works, although the separator is a vertical bar that has a special meaning ...
B4X:
Sub Activity_Click

    Dim str As String = "abc | def | efg"
    Dim x () As String = Regex.Split ("\ |", str)
@kisoft, the code is correct and works in project FlipCards without problems.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Ah! My bad! Sorry! I missed the array reference after the Regex.Split(...)(0) as it's not something I would ever do so I overlooked it. Also from the position of the caret in your post I thought it was _card_name that could not be located but I now see it is _gettag. I must have had an off period when I read your post :(

TappedCard is a B4XImageView which does not have an exposed Tag property which is the reason for the error. Has the type of TappedCard been changed from the original?

EDIT: Actually B4XImageView does have a Public Tag As Object field but it is being compiled as a gettag() method. I wonder what use Erel intended for that field :confused:
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
EDIT: Actually B4XImageView does have a Public Tag As Object field but it is being compiled as a gettag() method. I wonder what use Erel intended for that field :confused:
This code works as expected:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
 
    Private ImageView1 As B4XView
End Sub
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    ImageView1.Tag = "Ciao"
    Log(ImageView1.Tag)
End Sub



@stari post a small project, with just one card (two image files attached separately from the project).
 
Upvote 0

stari

Active Member
Licensed User
Longtime User
This code works as expected:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI

    Private ImageView1 As B4XView
End Sub
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    ImageView1.Tag = "Ciao"
    Log(ImageView1.Tag)
End Sub



@stari post a small project, with just one card (two image files attached separately from the project).
OK, @LucaMs .

B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    
    Private TappedCard As B4XImageView
    
    Private CardSize, Counter As Int
    Private LstCards As List
    Private MapRotation As Map
    Private B4XImgViewCard(12) As B4XImageView
    Private Angle, AddAngle As Int = 10 'AddAngle can be 5, 10 or 15
    Private xui As XUI
    Private CardBack, CardFace As B4XView
        
    Private pnlMain As Panel
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
    
    LstCards.Initialize
    MapRotation.Initialize
    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("Page3")
    
End Sub

Private Sub B4XPage_Appear
    PlaceCards
    Log("Create")
End Sub

'PLACE 12 CARDS ONTO THE SCREEN
Sub PlaceCards
    Dim kardc As Int = 1
    
    'Load card file names into the list
    For Each FileName As String In File.ListFiles(File.DirAssets)
        If FileName.EndsWith(".png") And Not(FileName.StartsWith("back.")) Then LstCards.Add(FileName)
    Next
    LstCards = ShuffleCards(LstCards) 'Shuffle the cards
    
    For Each cardname As String In LstCards
        Log("Card number:  " & kardc & "   Card Name:   " & cardname)
        kardc = kardc + 1
    Next
        
    Dim OffSetX, OffSetY, Rows = 3, Columns = 2 As Int
    CardSize = pnlMain.Width / 8
    OffSetX = (pnlMain.Width - CardSize * Columns - 10dip * (Columns - 1)) / 2
    OffSetY = (pnlMain.Height - CardSize * Rows - 10dip * (Rows - 1)) / 2
    CardBack = xui.LoadBitmapResize(File.DirAssets, "back.png", CardSize, CardSize, True) 'Load the flag back image

    'Place 2 x 6 shuffled flags onto a panel of 12 3x4 flags
    For X = 0 To Columns - 1
        For Y = 0 To Rows - 1
            B4XImgViewCard(Counter).Initialize(Me, "B4XImgViewCard")
            B4XImgViewCard(Counter).AddToParent(pnlMain, OffSetX + x * (CardSize + 10dip), OffSetY + y * (CardSize + 10dip), CardSize, CardSize)
            B4XImgViewCard(Counter).B4XBitmap = CardBack 'Set card back image
            B4XImgViewCard(Counter).Tag = $"${LstCards.Get(Counter)}~${Counter}"$ 'Store card name and card position

            MapRotation.Put(Counter, 0) 'Store card name and set rotation to 0
            Counter = Counter + 1
        Next
    Next
End Sub

'CLICK ON CARD ROTATE THE CARD
Sub B4XImgViewCard_OneClick

    Dim  slowd As Int = 50
    Dim card_name As String
        
    If Not(Angle = 0) Then Return 'Wait for card angle to be 0 before spinning the nect card
    TappedCard = Sender 'Create a sender to capture B4XImageView click
        
    'LogColor("Card:  " & TappedCard.Tag,Colors.Red)                'ime karte in številka karte od 0 - nn
    card_name = Regex.Split("_", TappedCard.Tag)(0).Replace("_", " ").Replace(".png", "")
    'card_name = Regex.Split("_", TappedCard.Tag)(0).Replace("_", " ").Replace(".png", "")
    LogColor(card_name,Colors.Blue)
    
    
    Dim CardRotation As Int = Regex.Split("~", TappedCard.Tag)(1) 'Read card position
    CardFace = xui.LoadBitmapResize(File.DirAssets, Regex.Split("~", TappedCard.Tag)(0), CardSize, CardSize, True) 'Load the card face image
    DisableCards(True) 'Disable cards

    If 180 = MapRotation.Get(CardRotation) Then Angle = 180  'Check last tapped card rotation position against the current angle and set the rotation angle
                
    For i = 0 To 180
        #If debug
        slowd= 1
        #end if       
        Sleep(slowd)
            
        Angle = Angle + AddAngle 'Set the angle
        B4XImgViewCard(CardRotation).Rotate(0, Angle + 180, 0) 'Rotate the card to the newly set angle

        Select Angle
            Case 90 'At 90°
                B4XImgViewCard(CardRotation).B4XBitmap = CardFace 'Set card face image
            Case 180 'At 180°                                        'če to izpustim, se karta zavrti za 360
                Angle = 0
                MapRotation.Put(CardRotation, 180) 'Set card rotation to angle
                DisableCards(False) 'Enable cards
                Log("End of Card_ON_Click - 180")
                Return
            Case 270 'At 270°
                B4XImgViewCard(CardRotation).B4XBitmap = CardBack 'Set card back image
                
            Case 360 'At 360°
                Angle = 0
                MapRotation.Put(CardRotation, 0) 'Set card rotation to 0
                DisableCards(False) 'Enable cards
                Log("End of Card_ON_Click - 360")
                Return
        End Select
        Sleep(0)
        
    Next
    
        
End Sub


'ENABLE OR DISABLE DORMANT CARDS
Sub DisableCards(SetCardsState As Boolean)
    For i = 0 To Counter - 1
        If Not(SetCardsState) Then
            B4XImgViewCard(i).B4XObject.Enabled = True
        Else
            B4XImgViewCard(i).B4XObject.Enabled = False 'Enable/Disable cards
        End If
    Next
End Sub

'SHUFFLE CARDS
Sub ShuffleCards(Cards As List) As List
    Dim ArrayVal As String
    Dim Random As Int
 
    For i = 0 To Cards.Size - 1
        Random = Rnd(i, Cards.Size)
        ArrayVal = Cards.Get(i)
        Cards.Set(i, Cards.Get(Random))
        Cards.Set(Random, ArrayVal)
    Next
    Return Cards
End Sub

Sub shuffle_Click
    pnlMain.RemoveAllViews
    Counter = 0
    LstCards.Initialize
    PlaceCards
End Sub
 

Attachments

  • 2_of_clubs.png
    2_of_clubs.png
    23 KB · Views: 154
  • back.png
    back.png
    42.7 KB · Views: 155
Upvote 0

stari

Active Member
Licensed User
Longtime User
Remove it, I meant just a sample non-working (with the bug you reported) project, not your fully project (which should be yours alone).


[However, the way to export a B4XPages project is to click on the link while holding down the CTRL key on the keyboard]
View attachment 104576
This is in fact @Erel - s project ThreePagesExample. Page 3 is substituted with FlipCard.
 
Upvote 0
Top