0

我正在为不同的 tetfields 和单选按钮设置共享首选项。我想在应用程序关闭并再次打开时清除这些字段,但要将这些首选项保留在磁盘上。在下面的代码中,当我取消此行时,名称被清除,但如果我对其他字段执行此操作,它会崩溃。如何清除预存储的 sharedpreference 的所有内容(不是存储的值)?

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);
    // When i take off this line the name is cleared but if i do it to other fields it crashes.
    name.setText(customSharedPreference.getString("namepref", ""));

    age = (EditText) findViewById(R.id.agefield);


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


    weight = (EditText) findViewById(R.id.weightfield);
    weight.setText(customSharedPreference.getString("weightpref", ""));

    height = (EditText) findViewById(R.id.heightfield);
    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()));


    Age = Integer.parseInt(age.getText().toString());
    Weight = Integer.parseInt(weight.getText().toString());
    Height = Integer.parseInt(height.getText().toString());

    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;

}
4

1 回答 1

0

在 oncreate 方法中将共享首选项编辑为 NULL 值

于 2012-04-24T20:14:09.537 回答