我该如何拯救我的应用程序的配置,而无需使用一个数据库??? (使用简单的文本文件) [英] How can i save the config of my app without using a database??? (using simple textfile)

查看:150
本文介绍了我该如何拯救我的应用程序的配置,而无需使用一个数据库??? (使用简单的文本文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要保存一个简单的领域configurate我的应用程序,导致此,我不会使用一个数据库(这只是一个字段...),我需要保存真或假值这个领域上的文件,并everytimes我的应用程序想要的部分检查,如果这是真的,他们要检查这个文本文件,而不是打开一个联接到数据库

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

我需要保存配置,直到永远......我的意思是,当我从我的应用程序退出,例如,我关闭了我的Andr​​oid设备,当我重新开始我的设备,并开始我的应用程序中,配置有保存

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

编辑:我有问题,第一个答案......这code是对我的onCreate方法:

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

推荐答案

我建议不要使用一个文本文件,但preference编辑器。

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

编辑:你必须初始化configEditor和设定值后,你必须提交

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

editor = settings.edit();
editor.putBoolean("name",true);
editor.commit();

这篇关于我该如何拯救我的应用程序的配置,而无需使用一个数据库??? (使用简单的文本文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆