Android Question List of "Type"

Hello every one

B4X:
Sub Globals

Dim lstEdges As List
Type Vertex(vName As String,Xcoordinate As Double,Ycoordinate As Double,Color As String, Forbiden As Boolean)
Dim sVertex As Vertex
Dim eVertex As Vertex

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MainLayout")
  
    lstEdges.Initialize
  
End Sub

Private Sub pnlGraph_Touch (Action As Int, X As Float, Y As Float)
if something then
    sVertex.vName="V1"
    sVertex.Xcoordinate=X1
    sVertex.Ycoordinate=Y1
else 
    eVertex.vName="V2"
    eVertex.Xcoordinate=X2    'different from X1'
    eVertex.Ycoordinate=Y2      'different from Y1'
end if
  
    lstEdges.Add((Array(sVertex,eVertex)))
  
End Sub
Log:

B4X:
Sub info
For i =0 To (lstEdges.Size)-1
     Dim V As Vertex=EdgeTable.Get(i)
     Log( " Name= " & V.vName &  " V.x= " & V.Xcoordinate & " V.y= " & V.Ycoordinate)
Next
end sub
So i receive something like this:
.............................
Name=v2 V.x= x2 V.y= y2
Name=v2 V.x= x2 V.y=y2
.............................

Where is the problem, please?
 
Last edited:
Solution
Hello every one

B4X:
Sub Globals

Dim lstEdges As List
Type Vertex(vName As String,Xcoordinate As Double,Ycoordinate As Double,Color As String, Forbiden As Boolean)
Dim sVertex As Vertex
Dim eVertex As Vertex

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MainLayout")
 
    lstEdges.Initialize
 
End Sub

Private Sub pnlGraph_Touch (Action As Int, X As Float, Y As Float)
if something then
    sVertex.vName="V1"
    sVertex.Xcoordinate=X1
    sVertex.Ycoordinate=Y1
else
    eVertex.vName="V2"
    eVertex.Xcoordinate=X2    'different from X1'
    eVertex.Ycoordinate=Y2      'different from Y1'
end if
 
    lstEdges.Add((Array(sVertex,eVertex)))
 
End Sub
Log:

B4X:
Sub info
For i =0 To (lstEdges.Size)-1...

DonManfred

Expert
Licensed User
Longtime User
Where is the problem, please?
you are reusing a global type.
Instead you should create a new one, fill the data and add the new reference to the list.

something like this?

B4X:
Sub Globals

Dim lstEdges As List
Type Vertex(vName As String,Xcoordinate As Double,Ycoordinate As Double,Color As String, Forbiden As Boolean)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MainLayout")
 
    lstEdges.Initialize
 
End Sub

Private Sub pnlGraph_Touch (Action As Int, X As Float, Y As Float)
if something then
Dim sVertex As Vertex
sVertex.Initialize
    sVertex.vName="V1"
    sVertex.Xcoordinate=X1
    sVertex.Ycoordinate=Y1
    lstEdges.Add(sVertex)
else
Dim eVertex As Vertex
eVertex.Initialize
    eVertex.vName="V2"
    eVertex.Xcoordinate=X2    'different from X1'
    eVertex.Ycoordinate=Y2      'different from Y1'
    lstEdges.Add(eVertex)
end if
 

 
End Sub
 
Upvote 1

ilan

Expert
Licensed User
Longtime User
Hello every one

B4X:
Sub Globals

Dim lstEdges As List
Type Vertex(vName As String,Xcoordinate As Double,Ycoordinate As Double,Color As String, Forbiden As Boolean)
Dim sVertex As Vertex
Dim eVertex As Vertex

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MainLayout")
 
    lstEdges.Initialize
 
End Sub

Private Sub pnlGraph_Touch (Action As Int, X As Float, Y As Float)
if something then
    sVertex.vName="V1"
    sVertex.Xcoordinate=X1
    sVertex.Ycoordinate=Y1
else
    eVertex.vName="V2"
    eVertex.Xcoordinate=X2    'different from X1'
    eVertex.Ycoordinate=Y2      'different from Y1'
end if
 
    lstEdges.Add((Array(sVertex,eVertex)))
 
End Sub
Log:

B4X:
Sub info
For i =0 To (lstEdges.Size)-1
     Dim V As Vertex=EdgeTable.Get(i)
     Log( " Name= " & V.vName &  " V.x= " & V.Xcoordinate & " V.y= " & V.Ycoordinate)
Next
end sub
So i receive something like this:
.............................
Name=v2 V.x= x2 V.y= y2
Name=v2 V.x= x2 V.y=y2
.............................

Where is the problem, please?

there is another problem in your code. you always add 2 object to the list but only 1 is initialized with any values because the if statement will allow only 1 object to get the new x,y coordinates and i understand that you want in this case to reuse the old value this is why you set it as a global variable. in this case you need to create new object and copy the value of the old object that is a global variable then update the global variable and everything should work fine.

something like this:

B4X:
Sub Globals
    Dim lstEdges As List
    Type Vertex(vName As String,Xcoordinate As Double,Ycoordinate As Double,Color As String, Forbiden As Boolean)
    Dim sVertex As Vertex
    Dim eVertex As Vertex
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MainLayout")
    lstEdges.Initialize
End Sub

Private Sub pnlGraph_Touch (Action As Int, X As Float, Y As Float)
    If something Then
        sVertex.vName="V1"
        sVertex.Xcoordinate=X
        sVertex.Ycoordinate=Y
    Else
        eVertex.vName="V2"
        eVertex.Xcoordinate=X    'different from X1'
        eVertex.Ycoordinate=Y     'different from Y1'
    End If
    lstEdges.Add((Array(CreateVertex(sVertex.vName,sVertex.Xcoordinate,sVertex.Ycoordinate,sVertex.Color,sVertex.Forbiden) _
                                    ,eVertex.vName,eVertex.Xcoordinate,eVertex.Ycoordinate,eVertex.Color,eVertex.Forbiden)))
End Sub

Public Sub CreateVertex (vName As String, Xcoordinate As Double, Ycoordinate As Double, Color As String, Forbiden As Boolean) As Vertex
    Dim t1 As Vertex
    t1.Initialize
    t1.vName = vName
    t1.Xcoordinate = Xcoordinate
    t1.Ycoordinate = Ycoordinate
    t1.Color = Color
    t1.Forbiden = Forbiden
    Return t1
End Sub

EDIT: there is no X1,Y1 and X2,Y2 only X and Y so i have change that too
 
Upvote 0
Solution
you are reusing a global type.
Instead you should create a new one, fill the data and add the new reference to the list.

something like this?

B4X:
Sub Globals

Dim lstEdges As List
Type Vertex(vName As String,Xcoordinate As Double,Ycoordinate As Double,Color As String, Forbiden As Boolean)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MainLayout")
 
    lstEdges.Initialize
 
End Sub

Private Sub pnlGraph_Touch (Action As Int, X As Float, Y As Float)
if something then
Dim sVertex As Vertex
sVertex.Initialize
    sVertex.vName="V1"
    sVertex.Xcoordinate=X1
    sVertex.Ycoordinate=Y1
    lstEdges.Add(sVertex)
else
Dim eVertex As Vertex
eVertex.Initialize
    eVertex.vName="V2"
    eVertex.Xcoordinate=X2    'different from X1'
    eVertex.Ycoordinate=Y2      'different from Y1'
    lstEdges.Add(eVertex)
end if
 

 
End Sub
B4X:
Private Sub pnlGraph_Touch (Action As Int, X As Float, Y As Float)
    
    
Dim blinkingTimer As Timer
blinkingTimer.Initialize("blinkingTimer",250)

Dim blnDrawPermision As Boolean
    '------------------------------- Vertices data retrieve -----------------------------------------
If Action=Activity.ACTION_UP Then
    If blnGetVertex=True Then
        blnGetEdge=False
    '------------------------------- Vertices add to list  -----------------------------------------
        Dim V As Vertex=CreateVertex("V" & lstV.Size ,X,Y,Colors.LightGray,False)       
            lstV.Add(V)
    '------------------------------- Vertices draw  -----------------------------------------

        cnv.Initialize(pnlGraph)
        cnv.DrawCircle(V.Xcoordinate,V.Ycoordinate,5dip,V.Color,True,1dip)
        cnv.DrawText(V.vName,V.Xcoordinate+10dip,V.Ycoordinate,Typeface.DEFAULT,10dip,Colors.BLACK,"LEFT")
        
    End If
        '------------------------------------ Edges data retrieve/correction-------------------------------------
    If blnGetEdge=True Then

        
        blnGetVertex=False
        If blnSVertexPositionied =False Then
            sVertexGloabl=lstV.Get(SnapNearestVertex(x,y))
            blnSVertexPositionied=True
            blnEVertexPositionied=False               
            blinkingTimer.Enabled=True
        Else           
            eVertexGloabl=lstV.Get(SnapNearestVertex(X,Y))           
            blnEVertexPositionied=True
            blnSVertexPositionied=False
            blnDrawPermision=True
            blinkingTimer.Enabled=False
            lblSelectOtherV.Enabled=False   
        End If
'----------------------------------------First Vertex data Retrieve                                     ----------------------------------------           
        If blnDrawPermision=True Then
            Dim sVertex As Vertex=sVertexGloabl
            Dim eVertex As Vertex=eVertexGloabl           
            cnv.Initialize(pnlGraph)
            cnv.DrawLine(sVertex.Xcoordinate,sVertex.Ycoordinate,eVertex.Xcoordinate,eVertex.Ycoordinate,Colors.Red,1dip)
            cnv.DrawCircle(sVertex.Xcoordinate,sVertex.Ycoordinate,4dip,Colors.LightGray,True,1dip)
            blnDrawPermision=False
                    
                lstEdges.Add((Array(sVertex,eVertex)))
                lstEdges.Add(Array(eVertex,sVertex))
                            
        End If
        
    End If
    
End If


End Sub


B4X:
Sub info

Dim i As Int
For i =0 To (lstEdges.Size)-1
    Dim Column() As Object=lstEdges.Get(i)
    Dim FirstV As Vertex=Column(0)
    Dim SecondV As Vertex=Column(1)
    Log( FirstV.vName &  " > " & SecondV.vName)       
Next
Snd sub

Result:
V0 > V1
V1 > V2
V2 > V3
V2 > V0
.
.
.


Thank you DonManfred.
The procedure is as follows:
1. Get some points
2. Draw points
3. Add them to a one-dimensional list.
Draw edges
4. Getting the approximate point and identifying the best point
5. Temporary storage of point coordinates
6. Getting the second point and identifying the best point
7. Draw an edge between the last two points.

As mentioned in number 5, temporary storage is needed. Considering that this piece of code is written in the touch routine, if the variable is local, the value of the variable will be lost.
My solution was to assign the value of the global variable to a local variable, then add it to the second list.
I welcome better ideas.
 
Upvote 0
Thank you, Ilan.
The problem was actually the pseudo code, not the real code, so I used x1, x2 ,...Of course, your guess about the reason for defining the variables as global was correct.
 
Upvote 0
Top