Android Question Parse GPX File (in Service) and ProgressDialog (not Show)

mw71

Active Member
Licensed User
Longtime User
Hi

i parse a File (GPX) in a Service and Write it to a SQLite DB. (Activity call the ParseGPXFile Sub)
The Code:
B4X:
Sub Process_Globals
    Dim parser As SaxParser
    Dim gpx_count, gpx_count_tmp As Long
    Dim lati, loni, SumName, cmt As String
    Dim sql_g_in As SQL
    Dim query_gpx As String
end sub

Sub ParseGPXFile
    '--- Parser
    If File.Exists(File.DirInternal,"Pos.db") = False Then
        sql_g_in.Initialize(File.DirInternal,  "Pos.db", True)
        query_gpx="CREATE TABLE Berge (id INTEGER PRIMARY KEY AUTOINCREMENT, Berg TEXT, Lat TEXT, Lon TEXT, Cmt TEXT, Loc Text)"
        sql_g_in.ExecNonQuery(query_gpx)
    Else
        If sql_g_in.IsInitialized =False Then sql_g_in.Initialize(File.DirInternal, "Pos.db", True)
    End If

    Dim In As InputStream = File.Openinput(File.DirInternalCache, "DownloadGPXFile.gpx")
    parser.Initialize
    parser.Parse(In,"parser")
    In.Close

    sql_g_in.Close
    ProgressDialogHide
    ToastMessageShow(Starter.loc.Localize("txt_fin"),True)

End Sub

'Parser Routinen
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
    If Name="wpt"  Then
        lati = Attributes.GetValue2("","lat")
        loni = Attributes.GetValue2("","lon")
    End If
End Sub
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If parser.Parents.IndexOf("wpt") > -1 Then
        If Name = "name" Then
            SumName= Text.ToString
        Else If Name = "cmt" Then
            cmt= Text.ToString
        End If
    End If

    Private m As Map

    If Name="sym" Then
        Dim Matcher1 As Matcher
        Dim loc As String
        Matcher1 = Regex.Matcher("[A-Z]{2}\d{2}[A-Z]{2}",cmt)

        Do While Matcher1.Find
            loc=Matcher1.Match
        Loop

            query_gpx="INSERT INTO Berge VALUES(NULL,?,?,?,?,?)"
            sql_g_in.ExecNonQuery2(query_gpx, Array As String(SumName,lati,loni,cmt.Replace(loc,""),loc))

        gpx_count=gpx_count+1
        gpx_count_tmp=gpx_count_tmp+1
        If gpx_count_tmp >=10 Then
            gpx_count_tmp=0
            CallSub2(Main,"ProgDialogAsync","Lade Summit # " & gpx_count)
        End If
    End If

End Sub

With ProgDialogAsync i will show ProgressDialog. Its ab Sub in the Main Activity.
The Code:
B4X:
Sub ProgDialogAsync(text As String) As ResumableSub
    Starter.cl.lg("ProgDialogAsync: " & text)
    ProgressDialogShow2(text,False)
#if release
    Sleep(0)
#end if
    Return True
End Sub

The Sub is Calling, unfortunately the Progressdialog is not showing.

What ist wrong or what can i do to show the User a "please Wait" with the actual status?
 

mw71

Active Member
Licensed User
Longtime User
Thanks Erel,

(Code, for dadabase) looks fine
B4X:
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
    If Name="wpt"  Then
        lati = Attributes.GetValue2("","lat")
        loni = Attributes.GetValue2("","lon")
    End If
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If parser.Parents.IndexOf("wpt") > -1 Then
        If Name = "name" Then
            SumName= Text.ToString
        Else If Name = "cmt" Then
            cmt= Text.ToString
        End If
    End If

    Private m As Map

    If Name="sym" Then
        Dim Matcher1 As Matcher
        Dim loc As String

        Matcher1 = Regex.Matcher("[A-Z]{2}\d{2}[A-Z]{2}",cmt)
        Do While Matcher1.Find
            loc=Matcher1.Match
        Loop
   
        query_gpx="SELECT * FROM Berge WHERE Berg LIKE '%" & SumName & "%'"

        m.Initialize
        m=DBUtils.ExecuteMap_nolog(sql_g_in,query_gpx,Null)

        If m.IsInitialized=False Then
            query_gpx="INSERT INTO Berge VALUES(NULL,?,?,?,?,?)"
            sql_g_in.AddNonQueryToBatch(query_gpx, Array(SumName,lati,loni,cmt.Replace(loc,""),loc))
        Else
            sql_g_in.AddNonQueryToBatch("UPDATE [Berge] SET [Berg]=?,[Lat]=?,[Lon]=?,[Cmt]=?,[Loc]=? WHERE [id] = ?", _
                                        Array(SumName,lati,loni,cmt.Replace(loc,""),loc,m.Get("id")))
        End If

        gpx_count=gpx_count+1
        gpx_count_tmp=gpx_count_tmp+1
        If gpx_count_tmp >=10 Then
            GPX_Prog_TXT="Lade Summit # " & gpx_count
            gpx_count_tmp=0
            CallSub2(Main,"ProgDialog",GPX_Prog_TXT)
        End If
    End If

and for start parsing and write to DB after parsing
B4X:
    Dim In As InputStream = File.Openinput(File.DirInternalCache, "DownloadGPXFile.gpx")
    parser.Initialize
    parser.Parse(In,"parser")
    In.Close

    Dim SenderFilter As Object = sql_g_in.ExecNonQueryBatch("SQL")
    Wait For (SenderFilter) SQL_NonQueryComplete (Success As Boolean)
    Starter.CL.Lg("NonQuery: " & Success)

unfortunately the Progressdialog is not showing also
 
Upvote 0

mw71

Active Member
Licensed User
Longtime User
Hi,

5943 Ticks for ~340 Positions in Release Mode

sri, i have boot Subs, the ProgDialog is without Sleep and Return. Boot the same result.

Testing in releas and debug mode.
- release mode, no ProgressDialog show
- debug mode, ProgressDialog is only Show with F8 or wenn i set a Brake Point
(give the UI Time to show/actualized)
 
Last edited:
Upvote 0

mw71

Active Member
Licensed User
Longtime User
hi,

the File is Parse in a Service, so i call ProgressDialogShow bevor Start Service and ProgressDialogHide after.
Works, but
- when Pharse the ProgressDialog is frozen (not spin)
- cant update (would be nice)
 
Upvote 0

mw71

Active Member
Licensed User
Longtime User
thanks for Anser
first i would solve the bottleneck and test this Code to Replace the DBUtils.ExecuteMap

B4X:
Dim SenderFil As Object = sql_g_in.ExecQueryAsync("SQL_EM", query_gpx,Null)

Wait For (SenderFil) SQL_EM_QueryComplete (Success As Boolean, rs As ResultSet)

If Success Then
m.Initialize

If rs.RowCount > 0 Then
  rs.Position=0
  for i = 0 To rs.ColumnCount- 1
     m.Put(rs.GetColumnName(i).ToLowerCase, rs.GetString2(i))
   Next
   rs.Close
End If
          
Log("M " & m)
          
          
If m.IsInitialized=False Then
  Log("Insert: " & SumName)
  query_gpx="INSERT INTO Berge VALUES(NULL,?,?,?,?,?)"
  sql_g_in.AddNonQueryToBatch(query_gpx, Array(SumName,lati,loni,cmt.Replace(loc,""),loc))
Else
  Log("Update: " & SumName & " /m " & m)
  sql_g_in.AddNonQueryToBatch("UPDATE [Berge] SET [Berg]=?,[Lat]=?,[Lon]=?,[Cmt]=?,[Loc]=? WHERE [id] = ?", _
                                            Array(SumName,lati,loni,cmt.Replace(loc,""),loc,m.Get("id")))
  End If
End If

unfortunately it works not (correkt)

- after Wait For the Parsing resume
- the m Map is empty


XML2MAP is the next to do (read the Tutorial....)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…