Android Question KeyValueStore trying to keep LIst after device reset.

Roger Daley

Well-Known Member
Licensed User
Hi All,

I am trying to keep the values stored in a list [History} during the operation of the program. KVS is supposed do to this but as you need to Initialize KVS at a restart it writes the SQL files afresh. I have tried to save the data file and the data-journal but it doesn't help. After a restart of the device I am always back to square one, Resume works as it should.
A text file representing part of the code is attached, hopefully someone can point me towards a solution.

Regards Roger
 

Attachments

Last edited:

derez

Expert
Licensed User
Do not do all the copy. Just try to initialize the kvs in a "safe" directory (like DirRootExternal with or without sub folder). Dirinternal is cleared in every installation of the app...
when initializing KVS it creates the data file if there isn't such file, or just read it for use.
 

Roger Daley

Well-Known Member
Licensed User
Hi derez,

KVS is already initialized in DirRootExternal.
The bottom line is that when KVS initializes, it overwrites the existing data file.
I tried to over come this by saving the files from DirRootExternal to DirInternal before initialization and writing them back to DirRootExternal immediately after initializing KVS.

The question comes back to: How do I stop the existing file being overwritten? My save and write back plan didn't work.


Regards Roger
 

James Chamblin

Active Member
Licensed User
You have several problems here. First is, you are making a copy from DirDefaultExternal to DirDefaultInternal, you are then initializing and modifying the DirDefaultExternal version. After that, you are copying the unmodified DirDefaultInternal version back over the modified version destroying any changes you have made. All this when your program first runs!

Second problem you have is that immediately after initializing the kvs, you are writing History to the kvs before History is even initialized or has any data. If there were anything there to begin with, you just overwritten it.

Basically, all you need to do is between If FirstTime Then and End If, get rid of everything except for the kvs.Initialize(File.DirDefaultExternal, "datastore"). Everything should work fine from then on unless there is another bug somewhere.
 

Roger Daley

Well-Known Member
Licensed User
Erel/Derez

Solution found, cause unknown.

I have been back and removed everything KVS. Started again, copying from the KVS sample as I did previously but all now works as it should. I have no idea what I did wrong originally and never will.

Thanks for the help once again, it really helps just posing the question and getting put back on track when assumptions go wild.

Regards Roger


PS James, your post came in after I first posted this. Thanks to you also.
 

Roger Daley

Well-Known Member
Licensed User
Please, edit your post as explained here.
Hi LucaMs

Regarding your post above about the "code" in my post, I would have asked this earlier but have been a bit pushed for time. I have grasped that I have breached some protocol/rule about spaces, I have followed the link in your post but unfortunately it didn't explain anything to me. My ignorance.
Please help me out here so I get it right in the future.

Firstly to show my total ignorance, what is meant by the term "Code Tag"? [Possibly what I know as "Comments Field"???]
Secondly it would be of great assistance if you could post a small example of incorrectly posted code and the corrected version. An example of few lines would probably be a better explanation than a thread of posts by people who understand the subject.

Thanks in advance Roger
[Educating the ingnorant is a noble cause.]
 

DonManfred

Expert
Licensed User
Firstly to show my total ignorance, what is meant by the term "Code Tag"? [Possibly what I know as "Comments Field"???]
I would usually say "Code-field" instead of Comment-Field...


See this




USE this feature in future when you want to post code here. Dont upload a txt-file. If you want to show us your complete code so we can download and test your app it is mostly better to do a Export as zip in the B4A Ide and upload this zip here then.

Use the Code-Tag and paste all the code into these dialogues...
This results in such a view of code:
B4X:
dim a as string
 

DonManfred

Expert
Licensed User
WRONG posted code:
lv.FastScrollEnabled = True
lv.SingleLineLayout.ItemHeight = 30dip
lv.SingleLineLayout.Label.TextSize = 12
lv.SingleLineLayout.Label.TextColor = Colors.Green
lv.SingleLineLayout.Label.Gravity = Gravity.CENTER
lv.TwoLinesLayout.ItemHeight = 60dip
lv.TwoLinesLayout.Label.TextSize = 10
lv.TwoLinesLayout.Label.TextColor = Colors.Red
lv.TwoLinesLayout.Label.Gravity = Gravity.CENTER
lv.TwoLinesLayout.SecondLabel.TextSize =12
lv.TwoLinesLayout.SecondLabel.TextColor = Colors.Red
lv.TwoLinesLayout.SecondLabel.Typeface = Typeface.DEFAULT_BOLD

For i = 0 To 49
lv.AddSingleLine(i)
Next



The same using Code-tags as i explained in the post before this:
B4X:
    lv.FastScrollEnabled = True
    lv.SingleLineLayout.ItemHeight = 30dip
  lv.SingleLineLayout.Label.TextSize = 12
  lv.SingleLineLayout.Label.TextColor = Colors.Green
  lv.SingleLineLayout.Label.Gravity = Gravity.CENTER
    lv.TwoLinesLayout.ItemHeight = 60dip
    lv.TwoLinesLayout.Label.TextSize = 10
    lv.TwoLinesLayout.Label.TextColor = Colors.Red
    lv.TwoLinesLayout.Label.Gravity = Gravity.CENTER
    lv.TwoLinesLayout.SecondLabel.TextSize =12
    lv.TwoLinesLayout.SecondLabel.TextColor = Colors.Red
    lv.TwoLinesLayout.SecondLabel.Typeface = Typeface.DEFAULT_BOLD
  
    For i = 0 To 49
        lv.AddSingleLine(i)
    Next
As you can see the code "using code-Tags" looks MUCH nicer and is MUCH easier to read here in forum.
 

Roger Daley

Well-Known Member
Licensed User
Thanks DonManfred,

Very enlightening, I had wondered why postings by others looked so impressive. I can now get in step.

B4X:
    Vibrate.Vibrate (50) ' Vibrate phone for 50 ms

Thanks very much for the help

Regards Roger
PS: It is the part of the line after the apostrophe that I know as "Comments Field".
 

DonManfred

Expert
Licensed User
Very enlightening, I had wondered why postings by others looked so impressive. I can now get in step.
glad i could help
Thanks very much for the help
Here in forum it you should click on the like-link in the bottom right region of a post. If there is any useful answer for you; click on like! <- It´s part of honor you give back to the posts Author. Likes will push the Authors reputation here in forum.

AND likes will help the forum-search-engine to find suiteable posts (including the answers..).

And, btw, it does NOT hurt to click on the like on an useful answer for you. It does not even cost any cent to you

Please note that i saw your answer and that you have learned from it. Your reputation is now a bit more (got a like from me due to your progress)

See this list to get the users with the most likes here
 
Last edited:
Top