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

给你一囗甜甜゛ 提交于 2019-12-21 17:25:22

问题


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


回答1:


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();


来源:https://stackoverflow.com/questions/4213960/how-can-i-save-the-config-of-my-app-without-using-a-database-using-simple-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!