Android Question writing and reading from device storage

GPJMA

Member
Licensed User
Longtime User
Hello All
I would be grateful for any advice on saving a list of items to the device internal storage.
I am trying to build a list in the app and displaying the items in a 'list view'.
This is all fine and it works well but I have tried without success to save this list so that it is persistent until the user edits or deletes it.
I bought the app development book and read the parts relating to this and tried the methods including using maps but I just cant get it to work.
I guess that I am doing something wrong but feeling a bit stupid is a small price to save what little hair I have.
I hope you can help.
Cheers
gpjma
 

GPJMA

Member
Licensed User
Longtime User
H
Thanks. I,ll do that.
I Appreciate your reply.


AS a thought. Many devices do not allow external storage of data. for instance the Samsung galaxy tabs etc. The external card can only be used for media. So is the dir.FileExternal still the option called for file storage?
 
Last edited:
Upvote 0

GPJMA

Member
Licensed User
Longtime User
Hello again all.
The random file access post did not help me with my problem.
It doesn't give any insight into what is happening or how to save my list to and from a listview object. It throws up more questions than answers.
The target device does not allow saving to external SD card. The posts that I read were all posted around 5 or 6 years ago before Samsung in their wisdom locked out the external SD card.I need to save to device memory. I also need to be able to read the file back into an object that can display the list.
Thanks All.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If the list is made of simple items (strings or numbers) then you can use File.WriteList / ReadList. Use File.DirInternal to store the files.

If the items are more complicated then you can use RandomAccessFile or B4XSerializator:
B4X:
Sub SaveList(list As List)
   Dim raf As RandomAccessFile
   raf.Initialize(File.DirInternal, "list.dat", False)
   raf.WriteB4XObject(list, 0)
   raf.Close
End Sub

Sub LoadList As List
   Dim raf As RandomAccessFile
   raf.Initialize(File.DirInternal, "list.dat", False)
   Dim res As List = raf.ReadB4XObject(0)
   raf.Close
   Return res   
End Sub
 
Upvote 0

GPJMA

Member
Licensed User
Longtime User
Hi.
Thanks Erel.
I will give this one a go. It is a simple list so I am sure it will be OK. I have not tried File.dirInternal.
I would like to try random file access as well.
Anyway Erel many thanks.
Grahame
 
Upvote 0

GPJMA

Member
Licensed User
Longtime User
Sub SaveList(listAsList)Dim raf AsRandomAccessFile
raf.Initialize(File.DirInternal, "list.dat", False)
raf.WriteB4XObject(list, 0)
raf.CloseEnd SubSub LoadList AsListDim raf AsRandomAccessFile
raf.Initialize(File.DirInternal, "list.dat", False)Dim res AsList = raf.ReadB4XObject(0)
raf.CloseReturn res End Sub


Hello Erel
I used the raf example you suggested (above) but I get the same problem both on the devices I am using for development and the emulator.
I get the message ":UNFORTUNATELY THE APP HAS STOPPED"
Any suggestions?
Cheers
Grahame
 
Upvote 0

GPJMA

Member
Licensed User
Longtime User
** Service (starter) Create **
** Service (starter) Start **
main_globals (java line: 395)
java.lang.NullPointerException: Attempt to invoke virtual method 'void anywheresoftware.b4a.objects.collections.List.Initialize()' on a null object reference
at anywheresoftware.b4a.samples.listview.main._globals(main.java:395)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.samples.listview.main.initializeGlobals(main.java:308)
at anywheresoftware.b4a.samples.listview.main.afterFirstLayout(main.java:99)
at anywheresoftware.b4a.samples.listview.main.access$000(main.java:17)
at anywheresoftware.b4a.samples.listview.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


Dim list As List
list.Initialize


Sub button3_click
Dim raf As RandomAccessFile
raf.Initialize(File.DirInternal, "list.dat", False)
raf.WriteB4XObject(list, 0)
raf.Close
End Sub


Hi Erel/All
Using this code the app on the virtual machine does not even start I just get the message on the machine that the application has stopped.
If I remove the initialization of LIST the app opens but closes when I tap button 3
In this case the log is telling me that LIST must be initialized.

EDIT
I have just moved the declaration and initialization of LIST to the button 3 code and the app does not crash and there are no error logs.
I have not yet checked if the file has been created but at least I don't get a crash.
Cheers
Grahame
 
Last edited:
Upvote 0

GPJMA

Member
Licensed User
Longtime User
Sorry I have never uploaded before so dont know the procedure. Also I dont really have a project. I am just trying code that I can build into a project.
I am trying to build a simple app to allow me to add text items to a listview (this is simple and is ok) I want to keep the list items in device memory so that when the app is closed and later restarted the items can populate a listview for editing or viewing.

I started using B4A many years ago with version 2 but never really needed to save and load files and I am surprised at just how complex this seems to be. Even Random access is way more complex than I expected.
If I can get a working example then I am sure that I can build what I need but I seem to be too inexperienced to grasp what is needed.
Anyway my friend if you can suggest something then that will be great otherwise I may need to resort to another platform for my little project.
Many thanks to All for the help.
Cheers
Grahame
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…