如何在android中的共享首选项中存储和加载布尔数组

时间:2015-03-04 09:27:03

标签: android android-layout android-activity android-preferences

朋友可以任何人检查此代码以共享首选项存储布尔数组,代码无法正常工作

  toggleButtonArray=new Boolean[47];

    for(int j=0;j<47;j++)
    {
        toggleButtonArray[j]=true;
    }
    SharedPreferences prefs_tbutton = getApplicationContext().getSharedPreferences("TogglePreference", 0);  
    SharedPreferences.Editor editor = prefs_tbutton.edit();  
    editor.putInt("tbPreferenceArray" +"_size", toggleButtonArray.length); 

    for(int i=0;i<toggleButtonArray.length;i++)
    {
        editor.putBoolean("tbPreferenceArray" + "_" + i, toggleButtonArray[i]);
      Toast.makeText(getApplicationContext(), "preference saved",Toast.LENGTH_LONG).show();

    }
    editor.commit();

加载布尔数组的代码是

public Boolean[] loadArray(String arrayName, Context mContext) {  


    SharedPreferences prefs_tbutton = mContext.getSharedPreferences("TogglePreference", 0);  
    int size = prefs_tbutton.getInt(arrayName + "_size", 0);  
    Boolean array[] = new Boolean[size];  
    for(int i=0;i<size;i++)  
        array[i] = prefs_tbutton.getBoolean(arrayName + "_" + i, false);  
    Log.e("ARRAY LOADING","SEEKING");

    return array;  
} 

请告诉我这段代码的错误是什么..............

code,loadArray

public class TimerService extends Service {


public  Boolean[] tbPreferenceArray;

 public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(TAG, "Service onStartCommand");


        new Thread(new Runnable() {
            @Override
            public void run() {


                tbPreferenceArray=loadArray("tbPreferenceArray",getApplicationContext());

0 个答案:

没有答案