empty items being saved in list view

variax20

Member
Hi everyone,

I seem to be having a problem when I run my code. For some reason, it seems that when I click my save button to save the list to the file, when I load the app up again and it loads the data, it's loading blank items into the list which I don't really want. Would anyone be able to shed some light on this at all please?
Many thanks!

My save button code:

B4X:
Sub saveBtn_Click
   'add an item to the scroll list
   noteList.AddSingleLine2("Note " & noteID, noteText.Text)
   ''Msgbox(noteText.Text,"")
   
   noteFilelist.Add(noteText.text)
   
   ''Msgbox("file contains: " & noteFilelist,"")
   ''ToastMessageShow("list currently contains "&noteList.Size, False)
   
   File.WriteList(File.DirRootExternal, "notes.txt", noteFilelist)
   
   noteID = noteID+1
End Sub


and my first-boot code:


B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layout")   
   noteList.Initialize("noteList")
   noteFilelist.Initialize
   noteID = 1
   
   If File.Exists(File.DirRootExternal, "notes.txt") Then
      noteFilelist = File.ReadList(File.DirRootExternal, "notes.txt")
      'Msgbox("file found. loaded "&(noteFilelist.Size/2), "")
      
      For i=0 To noteFilelist.Size-1
         If noteFilelist.Get(i) <> "" Then
            noteList.AddSingleLine2("Note " & noteID, noteFilelist.Get(i))
            noteID = noteID + 1
         Else
            noteFilelist.RemoveAt(i)
         End If
      Next
      ''Dim i As Int
      ''i=1
      
      ''Do While i < noteFilelist.Size-1
      ''   noteList.AddSingleLine2(noteFilelist.get(i),noteFilelist.Get(i+1))
      ''   i=i+2
      ''Loop
      
   Else
      Msgbox("no file found, creating new file", "")
      File.WriteList(File.DirRootExternal, "notes.txt",noteFilelist)
      'File.WriteList(File.DirRootExternal, "notes.txt",noteFilelist)
      ''Activity.AddView(noteList,0,30,100%x,20%y)
   End If 
   
   Activity.AddView(noteList,0,30,100%x,20%y)

End Sub


Cheers

Ash
 

variax20

Member
The problem is, for example

- enter 3 new notes and save each of them individually
- they appear of the list as:

Note 1 (with correct text)
Note 2 (with correct text)
Note 3 (with correct text)

exactly as i need them to be and when I click on each one they show me the saved text which is a value held in each item in the list.


Now when I close the app and load it again. I get the following:

Note 1 (with correct text)
Note 2 (blank)
Note 3 (with note 2's text)
Note 4 (blank)
Note 5 (with note 3's text)
note 6 (blank)

I haven't a clue why this is happening or how to fix it


Cheers

Ash
 

variax20

Member
Aha, it seems that when I'm entering some text into the textedit box, I hit enter twice, which gives me a new line (blank space) and this is what's being saved to the file. In other words it seems that if I enter more than one line of text in a multi line edittext box, it takes each lines and stores it into a separate list item.
What I'm looking for, is how to saved all the multi line text and as a single list item value. Any ideas please?


Thanks again

Ash
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
I have a PHP Script where I have to send some Tab Delimited Data to an application that stores multiline data that has a similar problem. When sending the data I replace the chars that are line feeds with another character (In my case I use an ASCII Form Feed Char, but could be anything not used in text). Then when reading it I change my Form Feed Chars back to line feeds.
 

variax20

Member
Thanks for your response Roger, it's much appreciated.
I'm not sure I follow what you mean with regards to the line feed chars etc.
Would you be able to demonstrate using some code at all? If that's possible


Thanks again

Ash
 

JonPM

Well-Known Member
Licensed User
Longtime User
Perhaps you would have an easier time using ReadMap/WriteMap instead of using lists. Each Key can be: "Note " & NoteID ...

$0.02
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…