Android:将布尔值保存到数据库(初学者)

时间:2012-03-23 18:01:51

标签: android database sqlite

我目前正在开发一款Android游戏,我有一个选项屏幕,目前有一个选项形式为ToggleButton:Music on或Music off。

我目前有这个布尔值,所以该类检查音乐当前是否正在播放,然后确定是否选中了ToggleButton:

    private boolean isMyServiceRunning(String serviceCanonicalClassName) {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceCanonicalClassName.equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

所以现在我希望能够将True或False的值发送到我设置的SQLite数据库。在当前这个时刻,我能做到这一点的唯一方法是创建一个TextView,它会改变值是否检查ToggleButton。然后,TextView中的值将成功发送到数据库,但这会产生一些问题,例如不保存用户选择的当前设置。

感谢所有回复的人,如果您需要更多代码来帮助您回答,我会尽快提供。

我发现一些答案非常令人困惑,所以我认为最好将我的所有代码复制到此处以便更好地理解:

public class OptionsActivity extends Activity {

private boolean isMyServiceRunning(String serviceCanonicalClassName) {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceCanonicalClassName.equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

Intent i; // Handles MyMusicService.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.options);


    final TextView tSound = (TextView) findViewById(R.id.textView2);
    final TextView tJoin = (TextView) findViewById(R.id.textView3);

    final Button saveBtn = (Button) findViewById(R.id.optSaveBtn);
    final Button tblBtn = (Button) findViewById(R.id.tableBtn);

    i=new Intent(this, MyMusicService.class);

    final ToggleButton soundOption = (ToggleButton) findViewById(R.id.soundPref);
    final ToggleButton joinOption = (ToggleButton) findViewById(R.id.joinPref);

    boolean musicPlays = isMyServiceRunning(MyMusicService.class.getCanonicalName());
    boolean joinChecking = joinOption.isChecked();

    soundOption.setChecked(musicPlays); 


    if(musicPlays==true){

        tSound.setText("On");
    }

    if(musicPlays==false) { 

        tSound.setText("Off");
    }

    if(joinChecking==true){

        tJoin.setText("Auto");
    }

    if(joinChecking==false){

        tJoin.setText("Manual");
    }

    soundOption.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {   

            // Perform action on clicks to control sound being on and off.   
            if(soundOption.isChecked()) {  

                Toast.makeText(OptionsActivity.this, "Music on.", Toast.LENGTH_SHORT).show(); 
                startService(i);

            } 

            else {  

                if(stopService(i)==true){
                    soundOption.setChecked(false);
                    stopService(i);
                    Toast.makeText(OptionsActivity.this, "Music off.", Toast.LENGTH_SHORT).show(); 

                }  
            }
        }});

    joinOption.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            if(joinOption.isChecked()){

                Toast.makeText(OptionsActivity.this, "Auto", Toast.LENGTH_SHORT).show();

            }

            else{

                Toast.makeText(OptionsActivity.this, "Manual", Toast.LENGTH_SHORT).show();

            }

        }
    });

    tblBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent shmoo = new Intent(OptionsActivity.this, SQLView.class);
            startActivity(shmoo);

        }
    });



    saveBtn.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {




            switch (v.getId()){ 


            case R.id.optSaveBtn: //Determine what will happen when the user presses the "Submit button".
                boolean optionsWork = true;
                try{

                    String sound = tSound.getText().toString();
                    String join = tJoin.getText().toString();

                    optionsDB entry = new optionsDB(OptionsActivity.this); //Creating a new instance of MasterMind game
                    entry.open();
                    entry.createOptionEntry(join, sound); //Passing both strings
                    entry.close();

                }catch (Exception e){ //Creating an error message if for some reason the app cannot transfer data to the Database.

                    Toast.makeText(OptionsActivity.this, "Error", Toast.LENGTH_SHORT).show();
                }

                finally { //Creating an AlertDialog box when the user presses the Submit button.

                    if (optionsWork){

                        Toast.makeText(OptionsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show();

                    }

                }
                break;
            }
        }
    });
}



protected void onDestroy() {
    if (this.isFinishing()){
        super.onDestroy();
        stopService(i);
    }
}

}

2 个答案:

答案 0 :(得分:6)

SQLite不处理布尔类型。我总是只使用smallint并在字段中放置1或0。确保将其设置为不接受空值,并根据您的用例将默认值设置为1或0。

这是一个例子:

CREATE  TABLE "db"."boolean_example" ("boolean_example_field" smallint NOT NULL  DEFAULT 0);

答案 1 :(得分:2)

要存储偏好,可能会使用Shared Preferences更好的想法。 这是一个例子:

    SharedPreferences settings = getSharedPreferences("GameSettings", 0);
    SharedPreferences.Editor editor = settings.edit();     
    editor.putBoolean("EnableMusic", mSilentMode);
    editor.commit();

    ...
    boolean isOn = settings.getBoolean("EnableMusic",true);