0

我正在制作一个需要存储 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);
    }
}
}
4

1 回答 1

1

看起来您在 onClick() 方法中不断将相同的 SharedPreference "checkboxValue" 设置为 true。

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

我建议:

  1. 在共享首选项中唯一地命名每个复选框。
  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();
        ...
    }
    
于 2014-07-08T19:44:51.260 回答