Android Question saving data and retrieving it later

cammel8

Member
Licensed User
Longtime User
OK I have been working on an app for a few weeks now. Basically what it is as a score keeper app for pool league. I have it saving all the pertinent info into variables right now. The app runs but when I shut it off I loose all the data.

I have looked into various ways of handling the information but cannot figure out which one to use.

My ultimate goal is to have the app update a file resident on the device every so many minutes so that if the app crashes they can pick up where they left off. then at the end of the night hit the submit button and transmit the data to me. I know it can be transmitted to a server like cloud kvs but i cannot afford a server at this time.

I would be OK with the app emailing me the file or even manually removing the file myself as the only devices this will be run on I have full control over and it will not be installed on any "Outside" devices.

OK so here is the basic gist of the app:
Two Players play each other. Based on their handicap they play x amount of games. The game count can be anywhere from one to ten games. Each game the following info has to be saved for later access by me so I can transfer it into a excel file to do the handicap calculations:
name of player one
name of player two
handicap of player one
handicap of player two
games needed for payer one
games needed for player two
makes for player one
makes for player two
misses for player one
misses for player two
defensives for player one
defensives for player two
made on break for player one
made on break for player two
left on table for player one
left on table for player two
dead balls for player one
dead balls for player two
winner of game

This data could be saved up to ten times in one usage before it gets submitted depending on the handicaps of the two players.

OK now my question. What is the safest and easiest way to go about implementing this idea???

As i stated before I cannot use servers unless i can build one that is completely free and reliable without the fear of losing data. I have looked at doing database files but not sure how to retrieve the data later. I have looked into saving it to text files but not sure how i would go about that either. About the best I have come up with so far is writing to excel file but I cant seem to get it to work and all the examples I have found seem to not work.

I don't need someone to do it for me but i do need ideas and tutorials or WORKING examples of those ideas. Any help would be appreciated. thanks in advance.
 

cammel8

Member
Licensed User
Longtime User
Create a custom type that holds all the data and save it with RandomAccessFile.WriteB4XObject.

I taught myself how to program so i'm not exactly sure what you just said. Can you point me to a tutorial somewhere that goes over what you said of point me to a example i can look at to see what i'm doing? Again any help is greatly appreciated.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type GoT(namePlayer1 As String, namePlayer2 As String, handicap1 As Int, Handycap2 As Int, gamesneeded1 As Int, gamesneeded2 As Int, makes1 As Int, makes2 As Int, misses1 As Int, misses2 As Int, defensives1 As Int, defensives2 As Int, madeonbreak1 As Int, madeonbreak2 As Int, left1 As Int, left2 As Int, deadballs1 As Int, deadballs2 As Int, winner As String)
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim game As GoT
    game.Initialize
    game.namePlayer1 = "DonManfred"
    game.namePlayer2 = "JohnDoe"
    ' fill all other values... work with this "game" till you need to save it.

Use RandomAccessFile lib to load/save such a game.
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type GoT(namePlayer1 As String, namePlayer2 As String, handicap1 As Int, Handycap2 As Int, gamesneeded1 As Int, gamesneeded2 As Int, makes1 As Int, makes2 As Int, misses1 As Int, misses2 As Int, defensives1 As Int, defensives2 As Int, madeonbreak1 As Int, madeonbreak2 As Int, left1 As Int, left2 As Int, deadballs1 As Int, deadballs2 As Int, winner As String)
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim game As GoT
    game.Initialize
    game.namePlayer1 = "DonManfred"
    game.namePlayer2 = "JohnDoe"
    ' fill all other values... work with this "game" till you need to save it.

Use RandomAccessFile lib to load/save such a game.
Thank you, so now i have all the variables into one type but how do i write it to a file then later read from the file?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Thank you, so now i have all the variables into one type but how do i write it to a file then later read from the file?
You are really lazy, aren´t you?

RandomAccessFile library: https://www.b4x.com/android/forum/threads/randomaccessfile-v1-30-writeencryptedobject.11566/
Tutorial: https://www.b4x.com/android/forum/t...ormation-with-randomaccessfile-library.11565/

As written in Post #2 by @Erel you should use WriteB4XObject and ReadB4XObject to write/read such a Type-Object to a file.

See documentation on RandomAccessLibrary for more info!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Yvon Steinthal

Active Member
Licensed User
Im not sure if im really understanding what you're looking for, but, i personally use StateManager to save information such as user preferences. You can put any key/value in a StateManager with SetSetting and SaveSetting and further get them using GetSetting(Key,Value).

Don't know if thats what you're looking for...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i personally use StateManager to save information such as user preferences
Yes it is a more complex solution.
To use StateManager you should choose Project - Add Existing Module and add StateManager.bas which is included in the attached example. You should also add a reference to the RandomAccessFile library and Reflection library.
StateManager is based on RandomAccesfile also...
RandomAccessFile should be fine for the issue here.

StateManager can be an alternative for sure.
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
Ok, now we now you are able to do Screenshots. But this does not really help here.
- You did a search for "raf" but the Objectname is RandomAccessFile and not raf. That is what you should have searched for.
The first result is the Link to the Library documentation. Here you are able to Navigate to the RandomAccessFile-documentation.
ReadB4XObject and WriteB4XObject documentation can be found accordingly.
The screen shots where to show, I'm not lazy. I'm trying to learn and showing I am doing searches and stuff. Just not finding what I need.

No I was searching randomaccessfile.Writeb4xobject which didn't bring up anything. Then random access file, Then random access file tutorial, Then random access file example, then writeb4xobject, then a butt load of other things that kept pointing to the same few posts that were of little help. Never thought of searching raf.
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
Offtopic:
While writing the code above - The Game of Types - i realized that GoT is an appreviation of Game of Thrones too ;-)

I forgot to mention "Winter is coming!" :D

Valar Morgulis
I was wondering if the GoT was a reference to game of thrones. But yeah in this instance I guess game of type is more fitting. Lol
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
You are really lazy, aren´t you?

RandomAccessFile library: https://www.b4x.com/android/forum/threads/randomaccessfile-v1-30-writeencryptedobject.11566/
Tutorial: https://www.b4x.com/android/forum/t...ormation-with-randomaccessfile-library.11565/

As written in Post #2 by @Erel you should use WriteB4XObject and ReadB4XObject to write/read such a Type-Object to a file.

See documentation on RandomAccessLibrary for more info!

So after reading everything, am I correct in this assumption?

A Random access file saves something to a text file then appends to the end of the last entry as you add more entries but never destroys or copies the file just keeps appending the file as needed. Like the old tape drives did. Only way to overwrite was to rewind. Is this correct?.

So I can add game one, then game two and so on and so forth to the same file as opposed to an excel file which creates a new instance of the file every time it "edits" the info.

I also read it cannot be read by a pc. So can I just save all game info into the raf then once the night is over and it is ready to be submitted just write all the info to a excel file. Or is there an easier way.

This raf thing does seem to be exactly what I'm looking for to hold the info resident on the device until it's ready to be retrieved.
 
Upvote 0
Top