android sharedPreferences设置和检索布尔值

时间:2014-10-15 11:56:41

标签: android boolean sharedpreferences

我想保存布尔值,然后在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();
        }
    }

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

4 个答案:

答案 0 :(得分:1)

您始终检查false的值init并且它不会进入您的if语句,因此prefs将不会更新。请执行此操作

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

并将其检查为

if(!init)

即。变化

boolean init = false;
    if (init) {

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

答案 1 :(得分:1)

这就是您需要使用共享偏好的方式。

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

以下是您在创建方法中所做的事情:

//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 :(得分:0)

您没有在上面的代码中使用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();
            }
        }

关键是:您需要先检查首选项值,然后相应地编码

答案 3 :(得分:0)

你也可以像这样使用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);
        }