如果应用程序重新打开共享preferences不节能布尔 [英] if application re-open SharedPreferences not saving boolean

查看:121
本文介绍了如果应用程序重新打开共享preferences不节能布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目,称为以下code menu.class

  F1 =(按钮)findViewById(R.id.f1);
f1.setOnClickListener(新View.OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
        意向意图=新的Intent();
        intent.setClassName(com.example.aplication,com.example.application.levelone);
        startActivityForResult(意向,0);
    }
});
}公共无效的onActivityResult(INT申请code,INT结果code,意图意图){
    super.onActivityResult(要求code,结果code,意向);    F2 =(按钮)findViewById(R.id.f2);
    f2lock =(ImageView的)findViewById(R.id.f2lock);    开关(结果code){
        案例11:f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);
    }
    共享preferences preferences = getShared preferences(preferences,MODE_PRIVATE);
    布尔levelTwoUnlocked = preferences.getBoolean(F2,真正的);    如果(levelTwoUnlocked){
        f2.setVisibility(View.VISIBLE);
        f2lock.setVisibility(View.GONE);
    }
    其他{
        f2.setVisibility(View.GONE);
        f2lock.setVisibility(View.VISIBLE);
    }    f2.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            意向意图=新的Intent();
            intent.setClassName(com.example.application,com.example.application.leveltwo);
            startActivityForResult(意向,0);
        }
    });
}

在code是工作的罚款。 F2 按钮设置可见和 f2lock 无形。
但是,当我重​​新打开应用程序的 F2 按钮回到可见。
难道我的preferences code不能完成?

更新时间:

我改变了preferences code这样的

 共享preferences preferences = getShared preferences(preferences,MODE_PRIVATE);
    共享preferences.Editor ED = preferences.edit();
    布尔levelTwoUnlocked = preferences.getBoolean(F2,真正的);
    ed.commit();

当我重新打开应用程序仍然有同样的问题。


解决方案

您检索 F2 的价值,但它并不像你保存任何地方。

  preferences.getBoolean(F2,真正的);

这将返回 F2 真正如果 f2的最后一次保存的值不存在(即你从来没有在你的共享preference实例保存它) - 它不会创建或保存在共享preferences实例的值你。

要保存的价值,你需要创建一个共享preferences编辑器,设置的值(或值)要保存并提交:

 共享preferences.Editor编辑器= preferences.edit();
editor.putBoolean(F2,levelTwoUnlocked);
editor.commit();

现在您下次阅读 F2 值时,就会产生什么价值,你上次提交。

I have the following code in my project called menu.class

f1 =(Button)findViewById(R.id.f1);      
f1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){
        // TODO Auto-generated method stub
        Intent intent = new Intent ();
        intent.setClassName ("com.example.aplication", "com.example.application.levelone");
        startActivityForResult (intent, 0);              
    }             
}); 
}

public void onActivityResult (int requestCode, int resultCode, Intent intent) {
    super.onActivityResult (requestCode, resultCode, intent);

    f2=(Button)findViewById(R.id.f2);      
    f2lock=(ImageView)findViewById(R.id.f2lock);

    switch (resultCode) {
        case 11: f2.setVisibility(View.VISIBLE);
                 f2lock.setVisibility(View.GONE);               
    }                                  
    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
    boolean levelTwoUnlocked = preferences.getBoolean("f2", true);

    if(levelTwoUnlocked){
        f2.setVisibility(View.VISIBLE);
        f2lock.setVisibility(View.GONE);
    }
    else {
        f2.setVisibility(View.GONE);
        f2lock.setVisibility(View.VISIBLE);
    } 

    f2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            Intent intent = new Intent ();
            intent.setClassName ("com.example.application", "com.example.application.leveltwo");
            startActivityForResult (intent, 0);              
        }             
    });     
}

The code is working fine. f2 button is set visible and f2lock invisible. But when I re-open the application the f2 button back to visible. Did my Preferences code not complete?

UPDATED

i had changed the preferences code like this

    SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
    SharedPreferences.Editor ed = preferences.edit();
    boolean levelTwoUnlocked = preferences.getBoolean("f2", true);
    ed.commit();                

and when i re-open the application it still had the same problems

解决方案

You are retrieving the value of f2, but it doesn't look like you're saving it anywhere.

preferences.getBoolean("f2", true);

This will return the last saved value of f2 or true if f2 doesn't exist (i.e. you've never saved it in your SharedPreference instance) - it won't create or save any value in the SharedPreferences instance for you.

To save the value, you need to create a SharedPreferences editor, set the value (or values) you want to save and commit it:

SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("f2", levelTwoUnlocked);
editor.commit();

Now the next time you read the f2 value, it will have whatever value you last committed.

这篇关于如果应用程序重新打开共享preferences不节能布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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