Android Question Help Please. Can ListView,File.WriteList ,File.ReadList work together?

giga

Well-Known Member
Licensed User
Longtime User
I am using ListView.
Once the user opens the app and starts a list it shows on the screen.
But if they close and reopen the app all input is gone.

B4X:
Sub Activity_Create(FirstTime AsBoolean)
Activity.LoadLayout("layout1")
ListView1.Initialize("ListView1")



If File.Exists(File.DirRootExternal,"test.txt") Then
List1 = File.ReadList(File.DirRootExternal, "test.txt")
Else

ListView1.AddSingleLine("Item #"& EditText1.Text)
List1.Sort(True)
File.WriteList(File.DirRootExternal, "test.txt", List1)

End If

Activity.AddView(ListView1, 0, 0, 100%x, 60%y)
End Sub

Sub ListView1_ItemClick (Position AsInt, Value AsObject)

Activity.Title = Value

List1.Initialize
ListView1.AddSingleLine("Item #"& EditText1.Text)
File.WriteList(File.DirRootExternal, "test.txt",List1)

I must be missing something....
I believe my missing link is the input is not getting into the test document or not staying when closed, but it shows on the screen:confused:

Thanks as Always
 

stevel05

Expert
Licensed User
Longtime User
You are updating ListView1 with the contents of EditText1, which is displayed on screen, but you are not updating List1. So you are loading and saving an empty list.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
I am updating the List1 now but running into an error: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3

example below: If I have a list of 5 rows but only 2 items were entered I get the above error. (seems to be coming from empty rows)
B4X:
For i=1ToList1.Size-1

ListView1.AddSingleLine(i & List1.Get(1))

ListView1.AddSingleLine(i & List1.Get(2))

ListView1.AddSingleLine(i & List1.Get(3))

ListView1.AddSingleLine(i & List1.Get(4))

ListView1.AddSingleLine(i & List1.Get(5))

Is there a better way?
Thanks to ALL.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
start counting from 0!

B4X:
For i=0 To List1.Size-1

If the size is 3 then the entries are: 0, 1, 2

Maybe i understood it not right what your problem is.

Maybe you show more code of what you have done and explain what exactly you want to do...

If I have a list of 5 rows but only 2 items were entered I get the above error. (seems to be coming from empty rows)
sure you just add 2 items. You Add the items 1 and 2. You did forget to add item 0

Listindex out of bound tells you that you are referencing to an index of a list which does not exist. Thats not "an empty line". thats "Non exiting lines"...

Your list has a List1.Size of 3. Then you can get the items 0, 1 and 2.

But you are reading items 4, 5 and 6 (Index 3, 4 and 5) from List1 which are not existent and thats calls the exception "Listindex out of bounds"

B4X:
List1.Get(3)
List1.Get(4)
List1.Get(5)

To add all List1-Entries into your ListView1 you have to write for example:

B4X:
For i=0 To List1.Size-1
  ListView1.AddSingleLine(i & List1.Get(i))
next
 
Last edited:
Upvote 0
Top