androidcheckboxsharedpreferencesandroid-checkboxcheckboxpreference

How to save data about multiple checkboxes using shared-preferences?


I am making an app for which I need to store the data of 60 check boxes. The data is read when the user presses the submit button. I want to display the user selection when the app is restarted.

I am trying to do it using the shared-preferences but don't know where I am making the mistake.

Y code is as follows:

import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent; 
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class Labs extends Activity implements OnClickListener {

ArrayList <Calendar> Cal= new ArrayList <Calendar>(60);

ArrayList <CheckBox> l= new ArrayList <CheckBox>(60);

ArrayList <Boolean> checkboxValue = new ArrayList <Boolean>(60);

private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mPrefsEditor;
private Button submitButton;



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


    submitButton = (Button)findViewById(R.id.button1);
    submitButton.setOnClickListener(this);
    mSharedPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
    mPrefsEditor = mSharedPreferences.edit();


    //Initialise check boxes for every slot
    for (int i = 101; i <= 160; i++) {
           String viewId = "checkBox" + i;
           l.add(i-101, (CheckBox) findViewById(getResources().getIdentifier(viewId, "id", getPackageName())));
           checkboxValue.add(i-101, mSharedPreferences.getBoolean("saveLogin", false));
        }   



    for(int j=0; j<60; j++)
    {
    if (checkboxValue.get(j) == true) {
       l.get(j).setChecked(true);}
   }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}   

public void addenable(){

}

public int Day(int a)
{
    int d=0;
    int x = a;
    x--;

    if(x>=30)
        d = (x/6)-3;

    else
        d=(x/6)+2;

    return d;
}

public int Hour(int a)
{
    int hr=0;
    int x=a;

    x--;

    hr=(x%6)+8;

    if((x%6)>=4)
        hr--;

    if(x>=30)
        hr = hr+6;

    return hr;

}

public int Min(int a)
{
    int m=0;
    int x=a;

    if(x%6==5)
        m=50;

    if(x%6==0)
        m=40;

    return m;
}

public void getdata(View V){

    for( int i=0; i<60; i++)
    {
        Cal.add(i, Calendar.getInstance());
        Cal.get(i).set(Calendar.DAY_OF_WEEK, Day(i+1));
        Cal.get(i).set(Calendar.HOUR_OF_DAY, Hour(i+1));
        Cal.get(i).set(Calendar.MINUTE, Min(i+1));
        Cal.get(i).set(Calendar.SECOND, 0);
    }


    //Intents
    Intent setVibration = new Intent();
    setVibration.setClass(this, AlarmReciever.class);

    Intent setNormal = new Intent();
    setNormal.setClass(this, RingerMode.class);

    //PENDING INTENTS
    ArrayList <PendingIntent> Lab_V= new ArrayList <PendingIntent>(60);
    ArrayList <PendingIntent> Lab_N= new ArrayList <PendingIntent>(60);

    for(int i=0; i<60; i++)
    {
        Lab_V.add(PendingIntent.getBroadcast(this, i, setVibration,
                PendingIntent.FLAG_UPDATE_CURRENT));
        Lab_N.add(PendingIntent.getBroadcast(this, i, setNormal,
                PendingIntent.FLAG_UPDATE_CURRENT));
    }

    // create the object
    AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    for(int i=0; i<60; i++)
    {
            if(l.get(i).isChecked()){

             mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Cal.get(i).getTimeInMillis(), 7*24*60*60*1000, Lab_V.get(i));
             mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Cal.get(i).getTimeInMillis()+50*60*1000, 7*24*60*60*1000, Lab_N.get(i));

             Toast.makeText(this, "L"+(i+1), Toast.LENGTH_SHORT).show();
            }
    }

    }


public void onClick(View view) {
    if (view == submitButton) {


       for(int i=0; i<60; i++)
       {

        if (l.get(i).isChecked()) {
            mPrefsEditor.putBoolean("checkboxValue" , true);
            mPrefsEditor.commit();
        } else {
            mPrefsEditor.clear();
            mPrefsEditor.commit();
        }
       }

        getdata(view);
    }
}
}

Solution

  • It looks like you are constantly setting the same SharedPreference "checkboxValue" to true in your onClick() method.

    I also don't understand exactly what your logic is in the onClick method, because it looks like if ANY of them are not checked, you are just clearing the whole SharedPreferences file.

    I'd recommend:

    1. Naming each of your CheckBoxes uniquely in shared preferences.
    2. Removing the mPrefEditor.clear() because you're just clearing the entire file, not a single line (unless that's what you want to do).

      public void onClick(View view) {
          ...
          for(int i = 0; i < 60 ; i++){
      
              if(l.get(i).isChecked()){
                  mPrefEditor.putBoolean("checkBox_"+i, true);
              }
              else {
                  mPrefEditor.putBoolean("checkBox_"+i, false);
              }
          }
          mPrefEditor.commit();
          ...
      }