Android:SharedPreferences不起作用

时间:2014-07-29 17:09:07

标签: java android sharedpreferences

我正在尝试实施SharedPreferences。 我已经搜索了它,尝试了近12个小时,它仍然无法解决我的问题。

Java代码

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    SharedPreferences.Editor editor = SharedP.edit();
    editor.putBoolean("banner_pref", true);
    editor.commit();
    Boolean myValue = SharedP.getBoolean("banner_pref", true);

    if (myValue == true){
        bannerfull = (RelativeLayout) findViewById(R.id.banner_full);
        bannerfull.setVisibility(View.VISIBLE);

        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();

    }

    else {

        bannerfull = (RelativeLayout) findViewById(R.id.banner_full);
        bannerfull.setVisibility(View.GONE);


        //SharedP.edit().putBoolean("banner_pref", false).commit();

    }
}

我知道我的SharedPreferences值是保持True。所以我的问题是,当我打开相同的Activity时,如何使其不正确?

如果有人能从我的代码中给我一个样本,我真的很喜欢它。所以我可以从中学习。

之前非常感谢。

2 个答案:

答案 0 :(得分:0)

我会抛出你的陈述:

editor.putBoolean("banner_pref", true);

进入else声明。如果if被点击,这将阻止它被设置,然后才能更改它。

答案 1 :(得分:0)

我刚刚对你的代码做了一些修改,它应该像这样工作

* Read the value with default is `true`, if the `SharedPreferences` doesn't exist before, certainly, it is the first time; `true` value is certain.
* if value is `true`, it is time to show the view; and then, update the value to `false` and save to `SharedPreferences`.
* else if value is `false`, hide the view.
* next time, when the `Activity` is launched, the value will be read as `false` (assuming there is none intervention in between, for example, users clear data from `Settings->Apps`...); the view is hidden, assuming there isn't any other action that update value to `true`.

这里是代码:

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // load sharedpref value 
    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    Boolean myValue = SharedP.getBoolean("banner_pref", true); // default: true

    // load layout, we just need it anyway
    bannerfull = (RelativeLayout) findViewById(R.id.banner_full);

    // if true, show it
    if (myValue == true) {
        bannerfull.setVisibility(View.VISIBLE);
        // ok, now switch value to 'false' and commit to SharedP
        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();
    }
    // if false, hide it
    else {
        bannerfull.setVisibility(View.GONE);
    }
}

更新:每三次显示一次。通过向计数器显示数量添加新值。

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // load sharedpref value 
    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    Boolean myValue = SharedP.getBoolean("banner_pref", true); // default: true
    int counter = SharedP.getInt("counter", 1); // default starting from 1

    if(counter == 3) {
      myValue = true;
      // counter reset
      counter = 0;
    }

    // load layout, we just need it anyway
    bannerfull = (RelativeLayout) findViewById(R.id.banner_full);

    // if true, show it
    if (myValue == true) {
        bannerfull.setVisibility(View.VISIBLE);
        // ok, now switch value to 'false' and commit to SharedP
        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();
    }
    // if false, hide it
    else {
        bannerfull.setVisibility(View.GONE);
    }

    // each time loading, increase the counter
    ++counter;
    editor = SharedP.edit();
    editor.putInt("counter",  counter);
    editor.commit();
}