Android Question lists to and from text file

harinder

Active Member
Licensed User
Longtime User
hello..i have 5 different edittexts with 5 different types of data entered by user. I am able to convert these data into 5 different lists. Now, how can I write these 5 different lists to a text file and later reuse and manipulate them and write back to same text file? Thanx
 

harinder

Active Member
Licensed User
Longtime User
hello..i have 5 different edittexts with 5 different types of data entered by user. I am able to convert these data into 5 different lists. Now, how can I write these 5 different lists to a text file and later reuse and manipulate them and write back to same text file? Thanx

i tried following:

For i = 1 To list1.Size-1
Dim MyLine As String = list1.Get(i)
writer.WriteLine(MyLine)
Next
For j = 1 To list2.Size-1
Dim yourLine As String = list2.Get(j)
writer.WriteLine(yourLine)
Next
writer.Close

not working:(
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Use code tags when posting code!

i suggest to use Randomaccessfile and here writeb4xobject and readb4xobject. Write list1 with writeb4xobject
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
Use code tags when posting code!

i suggest to use Randomaccessfile and here writeb4xobject and readb4xobject. Write list1 with writeb4xobject
Thanks Don for your reply...i am new to programming and will learn only with your patient teaching:)
I have been trying for a long time today without success. can you tell me where I am going wrong..
B4X:
Sub write
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    raf.Writeb4xobject(list1,0)
    raf.WriteB4XObject(list2,1)
    raf.close
End Sub

Sub Read
   
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    Dim list1 As List
    list1=raf.ReadB4XObject(0)
    For i = 0 To list1.Size-1                           
       Dim MyLine As String = list1.Get(i)
       Listview1.AddSingleLine(MyLine)
       spinner1.Add(MyLine)
    Next
   
    For j=0 To list2.Size-1                           
      Dim yourLine As String = list2.Get(i)
      listview2.AddSingleLine(yourLine)
    Next
    raf.Close
End Sub
       
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Dont bump! (at least not after a few hours) Be patient

I´m not sure if you can store two single lists in one RAF file in the way you do.
You can save each list into it´s own RAF file.
Or you can use a map to hold both lists and write the map to RAF.

B4X:
Sub write
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    dim m as Map
    m.initialize
    m.put("List1",list1)
    m.put("List2",list2)
    raf.Writeb4xobject(m,1)
    raf.close
End Sub

And do the opposite to read. Get the map from RAF. Get list1 and list2 from the Map. Note that you can use the Map to store more than just the lists... all into the same Object.
 
Last edited:
Upvote 0

harinder

Active Member
Licensed User
Longtime User
Dont bump! (at least not after a few hours) Be patient

I´m not sure if you can store two single lists in one RAF file in the way you do.
You can save each list into it´s own RAF file.
Or you can use a map to hold both lists and write the map to RAF.

B4X:
Sub write
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    dim m as Map
    m.initialize
    m.put("List1",list1)
    m.put("List2",list2)
    raf.Writeb4xobject(m,1)
    raf.close
End Sub

And do the opposite to read. Get the map from RAF. Get list1 and list2 from the Map. Note that you can use the Map to store more than just the lists... all into the same Object.
Dear Don..Thanks for your reply..i will be patient in future, just a little excited for solution:)

i used RAF to store 2 lists one one file..just a bit of tweaking and searching thanks to Erel"s post
https://www.b4x.com/android/forum/threads/randomaccessfile-v1-10-read-write-objects.8649/#post48195

B4X:
Sub write
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    raf.WriteObject(list1,False,raf.CurrentPosition)
    raf.WriteObject(list2,False,raf.CurrentPosition)
    raf.close
End Sub
B4X:
Sub Read
    list1.Initialize
    list2.Initialize
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    list1=raf.readObject(raf.CurrentPosition)
    list2=raf.readObject(raf.CurrentPosition)
    raf.Close
   
    For i = 0 To list1.Size-1                          
    Dim MyLine As String = list1.Get(i)
    Listview1.AddSingleLine(MyLine)
    spinner1.Add(MyLine)
    Next
    list2.Sort(True)
    For j=0 To list2.Size-1                         
    Dim yourLine As String = list2.Get(j)
    listview2.AddSingleLine(yourLine)
    Next
   
End Sub
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
The map solution is the best solution.
hello Erel..

I also tried map solution but without result..What may I be doing wrong here? Thanx

B4X:
Sub write
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    Dim m As Map
    m.initialize
    m.put(list1,"list1")
    m.put(list1,"list2")
    raf.Writeb4xobject(m,raf.currentposition)
    raf.close
End Sub


B4X:
Sub read
    list1.Initialize
    list2.Initialize
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    Dim m As Map
    m.initialize
    raf.readb4xobject(raf.currentposition)
    m.Get(list1)
    m.Get(list2)
   
    raf.close
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You are storing only ONE object to the file init.txt, right?

B4X:
Sub write
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    Dim m As Map
    m.initialize
    m.put(list1,"List1")
    m.put(list1,"List2")
    raf.Writeb4xobject(m,1)
    raf.close
End Sub

Note the Uppercase List1 and 2.... This is the KEY for the Map.

B4X:
Sub read
    list1.Initialize
    list2.Initialize
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    Dim m As Map
    m.initialize
    m = raf.readb4xobject(1) ' you did not assign the result to the map
    list1 = m.Get("List1") ' You was not using the result of m.get... !!
    list2 = m.Get("List2") ' same here
 
    raf.close
end sub
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
You are storing only ONE object to the file init.txt, right?

B4X:
Sub write
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    Dim m As Map
    m.initialize
    m.put(list1,"List1")
    m.put(list1,"List2")
    raf.Writeb4xobject(m,1)
    raf.close
End Sub

Note the Uppercase List1 and 2.... This is the KEY for the Map.

B4X:
Sub read
    list1.Initialize
    list2.Initialize
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    Dim m As Map
    m.initialize
    m = raf.readb4xobject(1) ' you did not assign the result to the map
    list1 = m.Get("List1") ' You was not using the result of m.get... !!
    list2 = m.Get("List2") ' same here

    raf.close
end sub
hello Don..

sorry typing error..i want to store 2 lists via map method..corrected write sub as follows
B4X:
Sub write
    raf.Initialize(File.DirRootExternal, "Init.txt", False)
    Dim m As Map
    m.initialize
    m.put(list1,"list1")
    m.put(list2,"list2")
    raf.Writeb4xobject(m,raf.currentposition)
    raf.close
End Sub
 
Upvote 0
Top