2012-04-24 3 views
0

以下のコードでは、ユーザーが属性のセットを入力してから、CaluculateCalories()を使用して保存して計算する必要があります。これは、保存するカロリーの数(Calnum) sharedpreferencesも。それはどのように可能ですか? ここにコードがあります。私はSavepreferencesにいくつかの行を追加する必要があります。計算された変数を共有プリファレンスに割り当てます

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.createprofile); 
    SharedPreferences customSharedPreference = getSharedPreferences(
      "myCustomSharedPrefs", Activity.MODE_PRIVATE); 

    name = (EditText) findViewById(R.id.namefield); 
    name.setText(customSharedPreference.getString("namepref", "")); 

    age = (EditText) findViewById(R.id.agefield); 
    // int Age = Integer.parseInt(age.getText().toString()); 
    age.setText(customSharedPreference.getString("agepref", "")); 

    genderradiogroup = (RadioGroup) findViewById(R.id.radioGroup2); 
    rb_male = (RadioButton) findViewById(R.id.maleradiobutton); 
    rb_female = (RadioButton) findViewById(R.id.femaleradiobutton); 
    genderradiogroup.check(customSharedPreference.getInt("genderprefs", 
      rb_male.getId())); 

    weight = (EditText) findViewById(R.id.weightfield); 
    // int Weight = Integer.parseInt(weight.getText().toString()); 
    weight.setText(customSharedPreference.getString("weightpref", "")); 

    height = (EditText) findViewById(R.id.heightfield); 
    // int Height = Integer.parseInt(height.getText().toString()); 
    height.setText(customSharedPreference.getString("heightpref", "")); 

    levelofactivity = (RadioGroup) findViewById(R.id.radioGroup3); 
    rb_light = (RadioButton) findViewById(R.id.lightradiobutton); 
    rb_moderate = (RadioButton) findViewById(R.id.moderateradiobutton); 
    rb_heavy = (RadioButton) findViewById(R.id.heavyradiobutton); 
    levelofactivity.check(customSharedPreference.getInt("levelpref", 
      rb_light.getId())); 

    Button addUser = (Button) findViewById(R.id.checkcreateprofilebutton); 

    addUser.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      savePreferences(); 

     } 
    }); 

} 

private void savePreferences() { 

    SharedPreferences customSharedPreference = getSharedPreferences(
      "myCustomSharedPrefs", Activity.MODE_PRIVATE); 
    SharedPreferences.Editor editor = customSharedPreference.edit(); 
    editor.putString("namepref", name.getText().toString()); 
    editor.putString("agepref", age.getText().toString()); 
    editor.putInt("genderprefs", genderradiogroup.getCheckedRadioButtonId()); 
    editor.putString("heightpref", height.getText().toString()); 
    editor.putString("weightpref", weight.getText().toString()); 
    editor.putInt("levelpref", levelofactivity.getCheckedRadioButtonId()); 

    editor.commit(); 
    finish(); 

} 


public float calculateCalories(int age, int weight, int height, String level) { 

    if (rb_male.isChecked()) { 

     bmr = 66.5f + (13.75f * weight) + (5.003f * height) 
       - (6.755f * age); 

     if (rb_light.isChecked()) { 
      calnum = bmr * 1.375f; 
     } 
     if (rb_moderate.isChecked()) { 
      calnum = bmr * 1.55f; 
     } 
     if (rb_heavy.isChecked()) { 
      calnum = bmr * 1.725f; 
     } 

    } else if (rb_female.isChecked()) { 

     bmr = 665 + (9.563f * weight) + (1.850f * height) - (4.676f * age); 

     if (rb_light.isChecked()) { 
      calnum = bmr * 1.375f; 
     } 
     if (rb_moderate.isChecked()) { 
      calnum = bmr * 1.55f; 
     } 
     if (rb_heavy.isChecked()) { 
      calnum = bmr * 1.725f; 
     } 

    } 
    return calnum; 

} 

}

答えて

1

あなたは、他のSharedPreference同じように設定します。何が問題になっていますか?何を試しましたか?

+0

他のすべての設定は、テキストフィールドまたはラジオボタンに入っているので、Editor.putStringを使用してそれらを保存しました。これはちょうど計算され、変数に格納されていますので、格納方法もわかりません。 – callback

+0

設定値のソースはエディタに関係ありません。検索された値と同じ型の適切なメソッドを使用して、計算された値をその中に入れることができます。 – dwemthy

関連する問題