【问题标题】:android sharedPreferences to set and retrieve boolean valueandroid sharedPreferences 设置和检索布尔值
【发布时间】:2014-10-15 11:56:41
【问题描述】:

我想保存布尔值,然后在 if-else 块中比较它们。但是当运行我的应用程序时,什么都没有显示。

我的代码在这里:

prefs = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
    editor = prefs.edit();
    boolean init = false;

    if (init) {
        Ion.with(this)
                .load("myurl")
                .asJsonArray().setCallback(new FutureCallback<JsonArray>() {

                    @Override
                    public void onCompleted(Exception arg0, JsonArray data) {
                        // TODO Auto-generated method stub

                        for (int i = 0; i < data.size(); i++) {

                            cat.setTitle(data.get(i).getAsJsonObject()
                                    .get("title").getAsString());
                            arrayList.add(cat);
                            db.addCategory(cat);
                            adapter.notifyDataSetChanged();
                        }
                        populateScroller();

                        editor.putBoolean("init", true).commit();
                    }
                });
    } else {
        dataSource = new CategoryDataSource(this);
        dataSource.open();

        arrayList = dataSource.getCategoryList();
        for (Categories c : arrayList) {
            c.getTitle();
        }
    }

所以我的问题是为什么共享首选项不起作用?请帮帮我。

【问题讨论】:

标签: android boolean sharedpreferences


【解决方案1】:

这就是您需要使用共享首选项的方式。

我将要展示的 senerio 是检查这个特定的活动是否是第一次运行。

这是您在 on create 方法中所做的:

//SharePrefernces
SharedPreferences appIntro = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    //Check if the Application is Running for the First time
    appIntro = getSharedPreferences("hasRunBefore_appIntro", 0);  //load the preferences
    Boolean hasRun = appIntro.getBoolean("hasRun_appIntro", false); //see if it's run before, default no
    //If FirstTime
    if (!hasRun) {
        //code for if this is the first time the application is Running
        //Display Activity
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_intro_activity);

        //Button to send Confirmation back 
        Button btn_ok = (Button) findViewById(R.id.appintro_btn_ok);
        btn_ok.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                //Save information that this application has run for the first time
                SharedPreferences settings = getSharedPreferences("hasRunBefore_appIntro", 0);
                SharedPreferences.Editor edit = settings.edit();
                edit.putBoolean("hasRun_appIntro", true);
                edit.commit(); //apply

                Intent intent = new Intent(Application_Intro_Activity.this, MainActivity.class);
                startActivity(intent);
                //close Activity
                finish();
            }
        });

    }//End of if(firstTime)
    else {
        //code if the Application has run before
        super.onCreate(savedInstanceState);
        //Navigate Directly to MainActivity
        Intent intent = new Intent(Application_Intro_Activity.this, MainActivity.class);
        startActivity(intent);
        //close Activity
        finish();
    }

}//End of OnCreate()

【讨论】:

    【解决方案2】:

    您始终检查initfalse 值并且它不会进入您的if 语句,因此prefs 不会更新。而是按照

     boolean init = prefs.getBoolean("init",false);
    

    并检查为

    if(!init)
    

    即改变

    boolean init = false;
        if (init) {
    

     boolean init = prefs.getBoolean("init",false);
     if(!init)
    

    【讨论】:

    • 谢谢老兄。我明白了。
    【解决方案3】:

    您没有在上述代码中使用SharedPreferences 值。您可能需要这样做:

    SharedPreferences prefs = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
            boolean prefValue = prefs.getBoolean("init", false);
            if(!prefValue)
            {
                // add value in SharedPreferences
                Ion.with(this)
                .load("myurl")
                .asJsonArray().setCallback(new FutureCallback<JsonArray>() {
    
                    @Override
                    public void onCompleted(Exception arg0, JsonArray data) {
                        // TODO Auto-generated method stub
    
                        for (int i = 0; i < data.size(); i++) {
    
                            cat.setTitle(data.get(i).getAsJsonObject()
                                    .get("title").getAsString());
                            arrayList.add(cat);
                            db.addCategory(cat);
                            adapter.notifyDataSetChanged();
                        }
                        populateScroller();
                        editor = prefs.edit();
                        editor.putBoolean("init", true).commit();
                    }
                });
            }
            else{
                dataSource = new CategoryDataSource(this);
                dataSource.open();
    
                arrayList = dataSource.getCategoryList();
                for (Categories c : arrayList) {
                    c.getTitle();
                }
            }
    

    关键是:你需要先检查偏好值并相应地编码

    【讨论】:

      【解决方案4】:

      你也可以像这样使用SharedPreferences

      /* In your onCreate method */
      SharedPreferences sp = getSharedPreferences(MyPrefs, Context.MODE_PRIVATE);
              if (!sp.getBoolean("first", false)) {
                  SharedPreferences.Editor editor = sp.edit();
                  editor.putBoolean("first", true);
                  editor.apply();
                  Intent intent = new Intent(this, IntroActivity.class); // Call the one tie launch java class
                  startActivity(intent);
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-03
        • 2011-03-20
        • 2015-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多