Store a value

fabero

Member
Licensed User
Longtime User
Hi All,

How I can use map to write some values, and then pushing on an "OK" button store the valuese, come back from main activity and use the values?

Next when i close and reopen the app, I want to retrieve the values and use them..

Any Advice/guidelines on that?
 
Last edited:

warwound

Expert
Licensed User
Longtime User
Hi.

I'd suggest using the JSON library.

Once you have populated the Map's values use the library to create a String representation of it.

That String is easy to save to a file and when you need to reload it the JSON library can re-create your Map.

The Map to JSON String (and vice versa) conversion requires just a few lines of code.

Look at the Documentaion pages for the JSON tutorial.

Martin.
 
Upvote 0

fabero

Member
Licensed User
Longtime User
Hi.

I'd suggest using the JSON library.

Once you have populated the Map's values use the library to create a String representation of it.

That String is easy to save to a file and when you need to reload it the JSON library can re-create your Map.

The Map to JSON String (and vice versa) conversion requires just a few lines of code.

Look at the Documentaion pages for the JSON tutorial.

Martin.

It's to much to do my stuff.. I need to store 2-3 values and reload it.

I see this:
http://www.b4x.com/forum/basic4andr.../8352-storing-user-preferences.html#post46900

but I can't figure.

I need to Load, another layout to permit to put values to the user, and next reload the principal layout or what else?
 
Upvote 0

fabero

Member
Licensed User
Longtime User
If you want to save a map to a file use File.WriteMap / ReadMap.

Yes I see.. But, is correct my idea?

I add 2 menuitem, Setting and Exit

In Exit, I put activity.finish

In setting sub i put something else

activity.loadlayout("settings")
file.Readmap for values

In the layout i Put textbox and label to permit put values
and next How I can comeback in the principal layout and store values?
Inserting a button, getting click event, and do

filemap.write and activity.loadlayout("main")

Or what else?
 
Upvote 0

fabero

Member
Licensed User
Longtime User
Sorry but I'm not sure that I understand what you are trying to do.
If you like to store your applications settings using a Map then WriteMap and ReadMap should be used.

Imagine a comune app. Menu button, click on settings.

There I can put and read settings and values previous stored, or predefined values. Change this values and next come back to main app.



But programmatically how to do that?

1) Add a menu item.

2) Clicking on them, from "main" layout open a new layout called "settings"

opening the new layout witch activity.loadlayout("settings")
use readmap to retrieve the values and populate textbox with stored values

3) Confirm/change some values and with an ok button to come back in "main" layout

do a writemap

activity.loadlayout("main")




Is correct this flow or not?
 
Upvote 0

fabero

Member
Licensed User
Longtime User
Your flow is correct.
You can also use InputMap for boolean properties.

Thanks!

Another option is to use PreferenceActivity: Basic4android - PreferenceActivity

I can not understand it. Is really tricky for me.. :BangHead::BangHead:


Another question: how to create the first time settings file where to load/save settings with read/write map; or I need to create and update all every time, and not update single value?
 
Last edited:
Upvote 0
Top