Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

android - Saving Checkbox states

I am very new and would appreciate if someone could demonstrate the code required to save a number of checkbox states in java inside of an android application.

Say i have a list of tools (Ten or more) a user needs to complete a task and would like them to be able to check off each one and have that data saved (within the app, not sQlite) so that it is recorded when they return to the application.

I have some idea of how this is done but really feel like i need to see the code to understand correctly.

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;

public class CheckBoxTest extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.checkboxtest);
    CheckBox cb1,cb2,cb3,cb4;

    cb1 = (CheckBox)findViewById(R.id.checkBox1);
    cb2 = (CheckBox)findViewById(R.id.checkBox2);
    cb3 = (CheckBox)findViewById(R.id.checkBox3);
    cb4 = (CheckBox)findViewById(R.id.checkBox4);   
     }
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the below code to store and retrive the data in SharedPreference. Your save each check box state in SharedPreference

//To get value from SharedPreference

SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
boolean value = preferences.getBoolean("KEY", false);
String value = preferences.getString("KEY");

//To Save value in SharedPreference

SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("KEY", value);
editor.putBoolean("KEY", value);
editor.commit();

look into it few minor changes::::

public class CheckBoxTest extends Activity implements OnCheckedChangeListener {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.favorites_add_button);
            CheckBox cb1,cb2,cb3,cb4;

            cb1 = (CheckBox)findViewById(R.id.checkBox1);
            cb1.setChecked(getFromSP("cb1"));
            cb1.setOnCheckedChangeListener(this);
            cb2 = (CheckBox)findViewById(R.id.checkBox2);
            cb2.setChecked(getFromSP("cb2"));
            cb2.setOnCheckedChangeListener(this);
            cb3 = (CheckBox)findViewById(R.id.checkBox3);
            cb3.setChecked(getFromSP("cb3"));
            cb3.setOnCheckedChangeListener(this);
            cb4 = (CheckBox)findViewById(R.id.checkBox4);
            cb4.setChecked(getFromSP("cb4"));
            cb4.setOnCheckedChangeListener(this);

        }
        private boolean getFromSP(String key){
        SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
        return preferences.getBoolean(key, false);
        }
        private void saveInSp(String key,boolean value){
        SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
        }
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            switch(buttonView.getId()){
            case R.id.checkBox1:
            saveInSp("cb1",isChecked);
            break;
            case R.id.checkBox2:
            saveInSp("cb2",isChecked);
            break;

            case R.id.checkBox3:
            saveInSp("cb3",isChecked);
            break;

            case R.id.checkBox4:
            saveInSp("cb4",isChecked);
            break;
            }

        }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...