我正在尝试设置个人资料,其中用户通过代表每个人的复选框选择男性或女性。然后我尝试使用SharedPreference对象保存此数据。已向两个复选框添加了onClickListener,如果选中,则将布尔值设置为true。
但无论我尝试了什么(我已经尝试了4小时!!!!!),当我刷新活动时,两个框都被检查,两个布尔值都是真的!任何想法都会很棒。
// check box initialisation male and female
male = (CheckBox) this.findViewById(R.id.cb_MalePrfofile);
male.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// when male is checked
if (((CheckBox) v).isChecked()) {
sex = "Male";
//malePref=true;
//femalePref=false;
female.setChecked(false);
Toast.makeText(getBaseContext(), "malePref=true", Toast.LENGTH_SHORT).show();
}
}
});// end on click for male
// if female check box is checked
female = (CheckBox) this.findViewById(R.id.cb_FemalePrfofile);
female.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// when male is checked
if (((CheckBox) v).isChecked()) {
sex = "Female";
//femalePref=true;
//malePref=false;
male.setChecked(false);
Toast.makeText(getBaseContext(), "feamlePref=true", Toast.LENGTH_SHORT).show();
}
}// end onClick/if
});// end on click call for male
}// end intilize widgets
设置SharedPref编辑器:
//Checkboxes
if(male.isChecked()){
//boolean maleValue=true;
editor.putBoolean("maleValue", true);
}else if(female.isChecked()){
//boolean femaleValue=true;
editor.putBoolean("femaleValue", true);
}//end else
onCreate方法根据保存的首选项设置数据:
male.setChecked(prefs.getBoolean("maleValue", false));
female.setChecked(prefs.getBoolean("femaleValue", false));
XML:
<CheckBox
android:id="@+id/cb_MalePrfofile"
android:text="Male"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></CheckBox>
<CheckBox
android:id="@+id/cb_FemalePrfofile"
android:layout_width="wrap_content"
android:text="Female"
android:typeface="serif"
android:layout_height="wrap_content"></CheckBox>
回答评论: 我已经编辑了editor.com(),其他所有来自微调器,编辑文本等的数据都保存得很好。
也尝试添加Radiogoup但是无论我选择哪个男性都默认选择加班活动重新加载。对于这里的长代码感到抱歉,但我发布的onCreate方法可能会遗漏一些内容:
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.profile);
intilizeWidgets();
//load teh sahred prefs object
prefs = this.getSharedPreferences(prefFilename, MODE_PRIVATE);
//SharedPreferences.Editor editor = prefs.edit();
//save teh sored values in the Edittexts
firstName.setText(prefs.getString("firstName", null));
lastName.setText(prefs.getString("lastName", null));
insuranceNo.setText(prefs.getString("insuranceNumber", null));
padiNumber.setText(prefs.getString("padiNum", null));
aboutMe.setText(prefs.getString("aboutMe", null));
boolean maleBoo = prefs.getBoolean("maleValue", false);
Toast.makeText(getBaseContext(), "Male checked is " + maleBoo, Toast.LENGTH_SHORT).show();
male.setChecked(prefs.getBoolean("maleValue", false));
female.setChecked(prefs.getBoolean("femaleValue", false));
boolean femaleBoo = prefs.getBoolean("femaleValue", false);
Toast.makeText(getBaseContext(), "FeMale checked is " + femaleBoo, Toast.LENGTH_SHORT).show();
certLevel.setSelection(prefs.getInt("certLevel", 0));
yearsExperince.setSelection(prefs.getInt("yearsExp", 0));
//radiobuttons
isMaleButton.setChecked(prefs.getBoolean("maleButton", false));
isFemaleButton.setChecked(prefs.getBoolean("femaleButton", false));
}// end onctreate
单击“保存”按钮后,我将获得共享首选项对象和编辑器
public void onClick(View arg0) {
//get the sahred pref object and its editor to accept values
this.prefs = this.getSharedPreferences(prefFilename, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
if(isMaleButton.isChecked()){
editor.putBoolean("maleButton", true);
}else if(isFemaleButton.isChecked()){
editor.putBoolean("femaleButton", true);
}
答案 0 :(得分:2)
似乎你缺少editor.commit()。
在不相关的说明中,为什么要使用复选框? radiobuttons似乎更贴切