如何使用共享首选项保存有关多个复选框的数据?

时间:2014-07-08 19:20:11

标签: android checkbox sharedpreferences android-checkbox checkboxpreference

我正在创建一个应用程序,我需要存储60个复选框的数据。 当用户按下提交按钮时,将读取数据。我想在重新启动应用程序时显示用户选择。

我正在尝试使用共享首选项,但不知道我在哪里犯错误。

Y代码如下:

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

1 个答案:

答案 0 :(得分:1)

看起来你经常设置相同的SharedPreference&#34; checkboxValue&#34;在你的onClick()方法中为true。

我也不明白你在onClick方法中的逻辑是什么,因为看起来如果没有检查它们中的任何一个,你只是清除整个SharedPreferences文件。

我建议:

  1. 在共享偏好设置中唯一地命名每个CheckBox。
  2. 删除mPrefEditor.clear(),因为您只是清除整个文件,而不是一行(除非您想要这样做)。

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