Save CheckBox State to SharedPreferences File in Android

孤街醉人 提交于 2019-12-12 23:50:27

问题


I have a couple of check boxes that I need to save so that when the user opens the Application again, they can see the state that they left the application in. I have tried using the preferences but I can't seem to get the result correctly.

MainActivity.java

package com.example.android.documentchecklist;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {

    CheckBox allotment, sscMarkList, hscMarkList, leaving, profomaCap, jeeScoreCard, gapCer, casteCer, casteVal, nonCreamyL, domicileCertificate, photograph, migration, adhaarCard, nationalityCertificate;
    boolean hasAllotment, hasSscMarkList, hasHscMarkList, hasLeaving, hasProfoma, hasJeeScore, hasGapCertificate, hasCasteCertificate, hasCasteValidity, hasNonCreamyLayer, hasDomicileCertificate, hasPhoto, hasMigCertificate, hasadhaarCard, hasNationalityCertificate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        allotment = (CheckBox) findViewById(R.id.allot);
        sscMarkList = (CheckBox) findViewById(R.id.ssc);
        hscMarkList = (CheckBox) findViewById(R.id.hsc);
        leaving = (CheckBox) findViewById(R.id.leaving);
        profomaCap = (CheckBox) findViewById(R.id.profoma);
        jeeScoreCard = (CheckBox) findViewById(R.id.jeeScore);
        gapCer = (CheckBox) findViewById(R.id.gapCertificate);
        casteCer = (CheckBox) findViewById(R.id.casteCertificate);
        casteVal = (CheckBox) findViewById(R.id.casteValidity);
        nonCreamyL = (CheckBox) findViewById(R.id.nonCreamyLayer);
        adhaarCard = (CheckBox) findViewById(R.id.adhaarCard);
        nationalityCertificate = (CheckBox) findViewById(R.id.nationalityCer);
        domicileCertificate = (CheckBox) findViewById(R.id.domicile);
        photograph = (CheckBox) findViewById(R.id.photo);
        migration = (CheckBox) findViewById(R.id.migration);

    }


    public void checkBoxClicked(View view) {

        int id = view.getId();

        if (id == R.id.mahaState) {

            migration.setVisibility(View.GONE);

            allotment.setText(getString(R.string.allot));
            allotment.setVisibility(View.VISIBLE);
            hasAllotment = allotment.isChecked();


            sscMarkList.setText(getString(R.string.ssc));
            sscMarkList.setVisibility(View.VISIBLE);
            hasSscMarkList = sscMarkList.isChecked();

            hscMarkList.setText(getString(R.string.hsc));
            hscMarkList.setVisibility(View.VISIBLE);
            hasHscMarkList = hscMarkList.isChecked();

            leaving.setText(getString(R.string.leaving));
            leaving.setVisibility(View.VISIBLE);
            hasLeaving = leaving.isChecked();

            profomaCap.setText(getString(R.string.proforma));
            profomaCap.setVisibility(View.VISIBLE);
            hasProfoma = profomaCap.isChecked();

            jeeScoreCard.setText(getString(R.string.jee));
            jeeScoreCard.setVisibility(View.VISIBLE);
            hasJeeScore = jeeScoreCard.isChecked();

            gapCer.setText(getString(R.string.gap_cert));
            gapCer.setVisibility(View.VISIBLE);
            hasGapCertificate = gapCer.isChecked();

            casteCer.setText(getString(R.string.caste_cert));
            casteCer.setVisibility(View.VISIBLE);
            hasCasteCertificate = casteCer.isChecked();

            casteVal.setText(getString(R.string.caste_validity));
            casteVal.setVisibility(View.VISIBLE);
            hasCasteValidity = casteVal.isChecked();

            nonCreamyL.setText(getString(R.string.non_creamy));
            nonCreamyL.setVisibility(View.VISIBLE);
            hasNonCreamyLayer = nonCreamyL.isChecked();

            adhaarCard.setText(getString(R.string.aadhar));
            adhaarCard.setVisibility(View.VISIBLE);
            hasadhaarCard = adhaarCard.isChecked();

            nationalityCertificate.setText(getString(R.string.nationality_cert));
            nationalityCertificate.setVisibility(View.VISIBLE);
            hasNationalityCertificate = nationalityCertificate.isChecked();

            domicileCertificate.setText(getString(R.string.domicile));
            domicileCertificate.setVisibility(View.VISIBLE);
            hasDomicileCertificate = domicileCertificate.isChecked();

            photograph.setText(getString(R.string.photos));
            photograph.setVisibility(View.VISIBLE);
            hasPhoto = photograph.isChecked();
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        save(allotment.isChecked());
        save(sscMarkList.isChecked());
        save(hscMarkList.isChecked());
        save(leaving.isChecked());
        save(profomaCap.isChecked());
        save(jeeScoreCard.isChecked());
        save(gapCer.isChecked());
        save(casteCer.isChecked());
        save(casteVal.isChecked());
        save(nonCreamyL.isChecked());
        save(domicileCertificate.isChecked());
        save(photograph.isChecked());
        save(migration.isChecked());
        save(adhaarCard.isChecked());
        save(nationalityCertificate.isChecked());
    }

    @Override
    public void onResume() {
        super.onResume();
        allotment.setChecked(load());
        sscMarkList.setChecked(load());
        sscMarkList.setChecked(load());
        hscMarkList.setChecked(load());
        leaving.setChecked(load());
        profomaCap.setChecked(load());
        jeeScoreCard.setChecked(load());
        gapCer.setChecked(load());
        casteCer.setChecked(load());
        casteVal.setChecked(load());
        nonCreamyL.setChecked(load());
        domicileCertificate.setChecked(load());
        photograph.setChecked(load());
        migration.setChecked(load());
        adhaarCard.setChecked(load());
        nationalityCertificate.setChecked(load());


    }

    private void save(final boolean isChecked) {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("check", isChecked);
        editor.apply();
    }

    private boolean load() {
        SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean("check", false);
    }

}

Thank You for your help.


回答1:


you are using the single key i.e. check to store your all checkbox state so only the state of this call save(nationalityCertificate.isChecked()); will be saved ,so you need to use different keys for different checkboxes

for example

    // use different keys to store state of different check boxes
    save(allotment.isChecked(),"allotment");
    save(sscMarkList.isChecked(),"sscMarkList");

    // use same keys to fetch values which were used during save function call
    allotment.setChecked(load("allotment"));
    sscMarkList.setChecked(load("sscMarkList"));


private void save(final boolean isChecked, String key) {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, isChecked);
    editor.apply();
}

private boolean load(String key) {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean(key, false);
}

Note: you can also initialize your shared preference like your CheckBox views in onCreate or onStart only once instead of re-initializing it every time in save



来源:https://stackoverflow.com/questions/41767248/save-checkbox-state-to-sharedpreferences-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!