Android Question Spliting the data in the next row in .txt file

Izmirov.Yaminovich

Member
Licensed User
Longtime User
Hi,

I have a sets of datas that look like this when I click on save:

0, 1, 2, 3, 4

Now when I click it again it goes stacking up like this:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

How do I make it so that when I click it it will start from another row instead of just doing it in a line? For example like this:

0, 1, 2, 3, 4
5, 6, 7, 8, 9

Are there any threads that I miss or not knowing the keywords in the search bar? Please guide me on this.

Thanks
 

Beja

Expert
Licensed User
Longtime User
I don't know what's in your Click code.. but you can append CRLF at the end of that code whatever it is.
 
Upvote 0

Izmirov.Yaminovich

Member
Licensed User
Longtime User
B4X:
Sub Save_click
    Dim str As String = EditText1.text & "," & EditText2.text & "," & Lat.Text & "," & Lon.Text & "," & Time.Text
    Dim writer As TextWriter
    writer.Initialize(File.OpenOutput(File.DirRootExternal,"data.txt",True))
    writer.WriteLine(str & CRLF)
    writer.Close
   
    Msgbox("Saved","")
End Sub

The code is as above. I've tried using CRLF, but it didn't start with another row though.
 
Upvote 0

Descartex

Well-Known Member
Licensed User
Longtime User
Try changing this:
B4X:
writer.WriteLine(str & CRLF)
With this:
B4X:
writer.WriteLine( CRLF & str)
 
Upvote 0

Dogbonesix

Active Member
Licensed User
Longtime User
Try

Sub Save_click
Dim str AsString = EditText1.text & "," & CRLF & EditText2.text & "," & CRLF & Lat.Text & "," & CRLF & Lon.Text & "," & CRLF & Time.Text
Dim writer AsTextWriter
writer.Initialize(File.OpenOutput(File.DirRootExternal,"data.txt",True))
writer.WriteLine(str & CRLF)
writer.Close
Msgbox("Saved","")
End Sub
 
Upvote 0

Izmirov.Yaminovich

Member
Licensed User
Longtime User
hmm...not working as well. is is because there's a problem with my loadfiles?
B4X:
Sub setToAddViews
    to_save_views(0) = EditText1
    to_save_views(1) = EditText2
    to_save_views(2) = Lat
    to_save_views(3) = Lon
    to_save_views(4) = Time
End Sub

Sub load
  If Not(File.Exists(File.DirRootExternal,"data.txt")) Then Return
  Dim txt As String = File.ReadString(File.DirRootExternal,"data.txt")
  txt = txt.Replace(" ", "")
  Dim str() As String = Regex.Split(CRLF,txt)
  If Not(str(str.Length-1).Contains(",")) Then Return
  Dim str2() As String = Regex.Split(",",str(str.Length-1))
  For i = 0 To to_save_views.Length-1
      to_save_views(i).Text = str2(i)
  Next
End Sub


Sub Save_click
    Dim str As String = EditText1.text & "," & CRLF & EditText2.text & "," & CRLF & Lat.Text & "," & CRLF & Lon.Text & "," & CRLF & Time.Text
    Dim writer As TextWriter
    writer.Initialize(File.OpenOutput(File.DirRootExternal,"data.txt",True))
    writer.WriteLine(str & CRLF)
    writer.Close
   
    Msgbox("Saved","")
End Sub
 
Upvote 0

Izmirov.Yaminovich

Member
Licensed User
Longtime User
B4X:
Sub SaveData(Items() As String)
Dim su As StringUtils
Dim rows as List = Array (Items)
su.SaveCSV(File.DirInternal, "data.txt", ",", rows)
End Sub

Or should I be using the stringutils instead? I was suggested to use this. But I don't really know how though.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
nope, not working. Weird, it should work in normal circumstances.

try this ... the data is written to file correctly
B4X:
Sub Save_Click
    Dim str As String = EditText1.text & "," & EditText2.text & "," & Lat.Text & "," & Lon.Text & "," & Time.Text 
    Dim writer As TextWriter
    writer.Initialize(File.OpenOutput(File.DirRootExternal,"data.txt",True))
    writer.WriteLine(str & CRLF)
    writer.Close
    Msgbox("Saved","")

    ReadFile

End Sub

Sub ReadFile
    Dim TextReader1 As TextReader
    TextReader1.Initialize(File.OpenInput(File.DirRootExternal, "data.txt"))
    Dim line As String
    line = TextReader1.ReadLine 
    Do While line <> Null
        Log(line) 'read the result to logs
        line = TextReader1.ReadLine
    Loop
    TextReader1.Close
End Sub
 
Upvote 0

Izmirov.Yaminovich

Member
Licensed User
Longtime User
Still not working though. I am opening this file in notepad on my laptop it just shows it in a straight line. But when I open it in my phone, it shows a vertical motion. I do prefer it to be working on the laptop though. Here's an attachment of my coding. Not really sure what's wrong.
 

Attachments

  • test.zip
    11.7 KB · Views: 249
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Also .. it you might want to get in the habit of giving views more descriptive names .. ie:

btnSave (button) edtLan or txtLan (EditText) lblTime (label)

You will definitely find this useful as you project grows.
 
Upvote 0

Izmirov.Yaminovich

Member
Licensed User
Longtime User
I'll look up to that. Any more to help in the problem I have? .txt files are not arranging accordingly as it is coded. There's must be something wrong with the code that I failed to notice.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
use windows based linefeed.

B4X:
Dim eol As String = Chr(13) & Chr(10)


Dim str As String = EditText1.text & "," & EditText2.text & "," & Lat.Text & "," & Lon.Text & "," & Time.Text & eol
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
here is a slightly revamped work of your project , using @sorex 's suggestion above...

As suggested it might be wise to look at using seperate Activities .. versus hiding/showing Layouts and panels
 

Attachments

  • Ecut.zip
    11.5 KB · Views: 227
Upvote 0
Top