2012-03-24 3 views
0

다른 액티비티에서 체크 박스의 부울 값을 사용/저장하려고했지만 많은 행운이 없었습니다.다른 액티비티 안드로이드에서 CheckBox 값 사용하기

나는 SharedPreferences를 사용해야하지만 올바르게 설정할 수는 없다는 것을 알고 있습니다. 나는 안드로이드에 새로운 오전과 사용하지 않은 예를 들어 옵션 메뉴에서 다음

private static final String OPTION_PREF = "my.main.project"; 
    private SharedPreferences optionPreferences; 
    private Editor optionEditor; 
    private boolean checkbox; 

    public Preferences(Context context) 
    { 
     this.optionPreferences = context.getSharedPreferences(OPTION_PREF, Activity.MODE_PRIVATE); 
     this.optionEditor = optionPreferences.edit(); 
    } 

    public boolean getChecked() 
    { 
     return optionPreferences.getBoolean("is_checked", checkbox); 
    } 
    public void saveChecked(boolean checkBox) 
    { 
     optionEditor.putBoolean("save_check_pref", checkBox); 
     optionEditor.commit(); 
    } 

그리고이있는 기본 클래스는,

boolean veggieChecked; 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.options); 

    Preferences pref; 
    pref = new Preferences(getApplicationContext()); 

    veggieChecked = pref.getChecked(); 

    final CheckBox checkBox = (CheckBox) findViewById(R.id.vegetarian); 
    if(checkBox.isChecked()) 
     veggieChecked = true; 

    pref.saveChecked(veggieChecked); 

난 정말 내가 잘못 뭐하는 거지 볼 수 없습니다 만든 전에 sharedpreferences .. 어떤 도움을 주시면 감사하겠습니다!

+0

을() 'checkbox.isChecked()'를 사용하는 것보다. – gobernador

답변

1

당신은 같은 것을 사용할 수 있습니다 : 당신이`SharedPreferences`를 사용하려는 경우, 당신은 Context.getSharedPreferencs()`다시 옵션 메뉴에서 다음 통화`수`를 호출해야합니다

CheckBox Equities = (CheckBox) findViewById(R.id.Equities); 
    CheckBox FixedIncome = (CheckBox) findViewById(R.id.FixedIncome); 
    CheckBox Currencies = (CheckBox) findViewById(R.id.Currencies); 
    CheckBox Commodities = (CheckBox) findViewById(R.id.Commodities); 
    CheckBox Derivatives = (CheckBox) findViewById(R.id.Derivatives); 



    myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); 
    prefsEditor = myPrefs.edit(); 


    int prefEq = myPrefs.getInt("Equities", 0); 
    int prefFI = myPrefs.getInt("FixedIncome", 0); 
    int prefCu = myPrefs.getInt("Currencies", 0); 
    int prefCo = myPrefs.getInt("Commodities", 0); 
    int prefDe = myPrefs.getInt("Derivatives", 0); 

    if(prefEq == 1) 
    { 
     Equities.setChecked(true); 
    } 
    else 
    { 
     Equities.setChecked(false); 
    } 


    if(prefFI == 1) 
    { 
     FixedIncome.setChecked(true); 
    } 
    else 
    { 
     FixedIncome.setChecked(false); 
    } 
    if(prefCu == 1) 
    { 
     Currencies.setChecked(true); 
    } 
    else 
    { 
     Currencies.setChecked(false); 
    } 
    if(prefCo == 1) 
    { 
     Commodities.setChecked(true); 
    } 
    else 
    { 
     Commodities.setChecked(false); 
    } 
    if(prefDe == 1) 
    { 
     Derivatives.setChecked(true); 
    } 
    else 
    { 
     Derivatives.setChecked(false); 
    } 






    Equities.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 
      prefsEditor.putInt("Equities", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 
      myArray[0] = false; 
      prefsEditor.putInt("Equities", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 


    FixedIncome.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 

      prefsEditor.putInt("FixedIncome", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 

      prefsEditor.putInt("FixedIncome", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 


    Currencies.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 

      prefsEditor.putInt("Currencies", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 

      prefsEditor.putInt("Currencies", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 




    Commodities.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 

      prefsEditor.putInt("Commodities", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 

      prefsEditor.putInt("Commodities", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 


    Derivatives.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 

      prefsEditor.putInt("Derivatives", 1); 
      prefsEditor.commit(); 
      } 
      else 
      { 

      prefsEditor.putInt("Derivatives", 0); 
      prefsEditor.commit(); 
      } 

     } 
    }); 
+0

게시물에 작은 변형이 생겨서 게시했습니다. 3에 의해 로버트, http://mgmblog.com/2008/02/18/android-checkbox-oncheckedchangelistener/, 나는이 진술을 먼저 가지고 있었다 SharedPreferences myPrefs; myPrefs = this.getSharedPreferences ("myprefs", MODE_WORLD_READABLE); 최종 편집기 prefsEditor = myPrefs.edit(); 최종 CheckBox checkBox = (CheckBox) findViewById (R.id.vegetarian); checkBox.setChecked (myPrefs.getBoolean ("veggieBox", false))); – TomSelleck

1

처음에는 키를 넣고 나중에 save_check_pref을 가져 오려고합니다.

관련 문제