2017-01-20 4 views
1

Ich habe ein paar Kontrollkästchen, die ich speichern muss, so dass, wenn der Benutzer die Anwendung erneut öffnet, können sie den Status sehen, dass sie die Anwendung in. Ich habe versucht mit der Vorlieben, aber ich kann nicht scheinen, das Ergebnis richtig zu bekommen.Speichern CheckBox State zu SharedPreferences Datei in Android

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); 
    } 

} 

Vielen Dank für Ihre Hilfe.

Antwort

2

Sie verwenden die einzelnen key dh check speichern alle Ihre checkbox Zustand, so dass nur der Staat dieser Aufforderung save(nationalityCertificate.isChecked()); werden gespeichert, so dass Sie verschiedene keys für verschiedene checkboxes

zum Beispiel

// 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); 
} 
verwenden müssen

Hinweis: Sie können Ihre gemeinsamen Einstellungen wie CheckBox views in onCreate oder onStart auch nur einmal initialisieren, anstatt sie jedes Mal erneut inzu initialisieren

+0

Das hat funktioniert! Ich habe SharedPreferences noch nie zuvor benutzt. Danke für die schnelle Antwort. Ich habe gerade meine Methodendefinition geändert und verschiedene Schlüssel zum Speichern des Status von Kontrollkästchen verwendet. –

+0

@VarunJoshi Ich bin froh, dass ich helfen konnte, glückliche Codierung :) –