Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i save the config of my app without using a database??? (using simple textfile)

i need to save a simple field to configurate my APP, cause this, i wont use a database (it's only a field...), i need to save true or false value for this field on a file, and everytimes a section of my app wanna check if it is true they have to check this textfile, and not to open a connexion to a database

i need to save the config for ever... i mean that when i exit from my app, and for example, i shut down my android device, when i start my device again and start my app, the config have to be saved

is this possible? how can i do it? i can't find any information about that

EDIT: i have problems with the first answer... this code is on my oncreate method:

static SharedPreferences settings;
static SharedPreferences.Editor configEditor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);

    if (settings.getBoolean("showMeCheckBox", true)) 
     showMeCheckBox.setChecked(true);
    else 
     showMeCheckBox.setChecked(false);

applyButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
             if (showMeCheckBox.isChecked()) {
                 configEditor.putBoolean("showMeCheckBox", true); 
                } else {
                 configEditor.putBoolean("showMeCheckBox", false);
                }

            }
});

ok, but this doesn't works... allways is selected... always true, like the default value... doesn't matter if i checked or unchecked it.... :S

like image 340
NullPointerException Avatar asked Nov 18 '10 10:11

NullPointerException


People also ask

How can we store data without using database in C#?

you can use xml serialization or binary serialization.

How is application data stored?

The ways for storing such data are as follows:Databases. Structured storages. Archives (as a specific form of structured storage) Remote (distributed, cloud) storages.

How do you create a data text file?

Another way to create a text file is to right-click an empty area on the desktop, and in the pop-up menu, select New, and then select Text Document. Creating a text file this way opens your default text editor with a blank text file on your desktop. You can change the name of the file to anything you want.

How do I convert a text file to an Access database?

Access opens the Get External Data – Text File dialog box. In the Get External Data - Text File dialog box, specify the name of the text file that contains the data to which you want to link in the File name box. Select Link to the data source by creating a linked table and then click OK.


1 Answers

i suggest not to use a textfile but the Preference Editor.

static SharedPreferences settings;
static SharedPreferences.Editor editor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
editor = settings.edit();
//store value
editor.putString("Preference_name_1", "1");
//get value
//eill return "0" if preference not exists, else return stored value
String val = settings.getString("Preference_name_1", "0");

Edit: you have to initialize the configEditor and after setting a value, you have to commit

editor = settings.edit();
editor.putBoolean("name",true);
editor.commit();
like image 120
2red13 Avatar answered Sep 18 '22 09:09

2red13