2012-04-24 118 views
1

我想從下面定義的sharedpreference中加載Name的值,但我無法加載。我如何提取Name的值並敬酒或理想地將它放在文本字段中?無法從sharedpreferences獲取值

public class FillingActivity extends Activity{ 

TextView username; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.foodandexercise); 

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 

    String user = prefs.getString("namepref", ""); 
    Toast.makeText(FillingActivity.this, user, Toast.LENGTH_LONG).show(); 

    username = (TextView) findViewById(R.id.loggedastoedit); 
    username.setText("najib"); 




} 

}

,這裏是我的SharedPreferences

公共類ProfilePreferenceActivity延伸活動{

DatabaseAdapter usertable = new DatabaseAdapter(this); 

float calnum, bmr; 
String level, gender; 
int Age, Height, Weight, temp; 
RadioButton rb_male; 
RadioButton rb_female; 
RadioButton rb_light; 
RadioButton rb_moderate; 
RadioButton rb_heavy; 
EditText name; 
EditText age, weight, height; 
RadioGroup genderradiogroup; 
RadioGroup levelofactivity; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.createprofile); 
    SharedPreferences customSharedPreference = getSharedPreferences(
      "myCustomSharedPrefs", Activity.MODE_PRIVATE); 

    name = (EditText) findViewById(R.id.namefield); 
    name.setText(""); 
    age = (EditText) findViewById(R.id.agefield); 
    age.setText(""); 

    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())); 
    genderradiogroup.check(customSharedPreference.getInt("genderprefs", 
      rb_male.getId())); 

    weight = (EditText) findViewById(R.id.weightfield); 
    weight.setText(""); 
    height = (EditText) findViewById(R.id.heightfield); 
    height.setText(""); 

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

    try { 
     Age = Integer.parseInt(age.getText().toString()); 
     Weight = Integer.parseInt(weight.getText().toString()); 
     Height = Integer.parseInt(height.getText().toString()); 
    } catch (Exception e) { 
    } 

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

    addUser.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      savePreferences(); 

      float calnumber = calculateCalories(Age, Weight, Height); 
      String lala = String.valueOf(calnumber); 
      Toast.makeText(ProfilePreferenceActivity.this, lala, 
        Toast.LENGTH_LONG).show(); 

      usertable.open(); 
      long id = usertable.createUser(name.getText().toString(), age 
        .getText().toString(), gender, weight.getText() 
        .toString(), height.getText().toString(), level, 
        calnumber); 

      Toast.makeText(ProfilePreferenceActivity.this, 
        "user added with" + level + gender, Toast.LENGTH_LONG) 
        .show(); 

      usertable.close(); 

      Intent Filling = new Intent(); 
      Filling.setClass(ProfilePreferenceActivity.this, 
        FillingActivity.class); 
      startActivity(Filling); 

     } 
    }); 

} 

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.putFloat("calpref", calnum); 
    editor.commit(); 
    finish(); 

} 

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

    if (rb_male.isChecked()) { 

     gender = "male"; 

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

     if (rb_light.isChecked()) { 
      level = "light"; 
      calnum = bmr * 1.375f; 
     } 
     if (rb_moderate.isChecked()) { 
      level = "moderate"; 
      calnum = bmr * 1.55f; 
     } 
     if (rb_heavy.isChecked()) { 
      level = "heavy"; 
      calnum = bmr * 1.725f; 
     } 

    } else if (rb_female.isChecked()) { 

     gender = "female"; 

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

     if (rb_light.isChecked()) { 
      level = "light"; 
      calnum = bmr * 1.375f; 
     } 
     if (rb_moderate.isChecked()) { 
      level = "moderate"; 
      calnum = bmr * 1.55f; 
     } 
     if (rb_heavy.isChecked()) { 
      level = "heavy"; 
      calnum = bmr * 1.725f; 
     } 

    } 
    return calnum; 

} 

}

+0

我想你應該在兩個地方都使用'getDefaultSharedPreferences()'或'getSharedPreferences(「myCustomSharedPrefs」,...)'。這樣你創建(AFAIK)不同的共享偏好。 – zapl 2012-04-24 22:31:49

+0

想要將其移至答案@zapl?這是解決方案。 – 2012-04-24 22:36:57

+0

我現在明白了。非常感謝你,你可以把它移到答案,我會驗證。 – callback 2012-04-24 22:38:00

回答

1

您應在這兩個地方使用getDefaultSharedPreferences()getSharedPreferences("myCustomSharedPrefs",...)

通過這種方式,您可以創建(AFAIK)不同的共享首選項,並且無法讀取您寫入其他數據的數據。