嗨,当我保存信息时,无论我用名字写什么,总是将“ dancer单选按钮”保存,我在“姓氏”中写的内容都会复制到“名字字段”中。 我认为问题是我的活动,我不知道我写了什么= /
这是活动:
public class user_profile extends AppCompatActivity {
private user_sample userSample = new user_sample();
user_sharedpref_manager user_sharedpref_manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
user_sharedpref_manager = new user_sharedpref_manager(this);
userSample = user_sharedpref_manager.get_user();
EditText profile_firstname = findViewById(R.id.profile_firstname);
EditText profile_lastname = findViewById(R.id.profile_lastname);
RadioButton profile_gamer_button = findViewById(R.id.profile_gamer_radio_button);
RadioButton profile_dancer_button = findViewById(R.id.profile_dancer_radio_button);
RadioButton profile_soccer_player_button = findViewById(R.id.profile_soccer_player_radio_button);
Button profile_save_button = findViewById(R.id.profile_save_button);
profile_firstname.setText(userSample.getFirstname());
profile_lastname.setText(userSample.getLastname());
profile_firstname.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
userSample.setFirstname(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
profile_lastname.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
userSample.setLastname(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
int career = userSample.getCareer();
if (career == user_sample.gamer) {
profile_gamer_button.setChecked(true);
} else if (career == user_sample.dancer) {
profile_dancer_button.setChecked(true);
} else if (career == user_sample.soccer_player) {
profile_soccer_player_button.setChecked(true);
}
profile_gamer_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
userSample.setCareer(user_sample.gamer);
}
});
profile_dancer_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
userSample.setCareer(user_sample.dancer);
}
});
profile_soccer_player_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
userSample.setCareer(user_sample.soccer_player);
}
});
profile_save_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
user_sharedpref_manager.save_user(userSample);
Toast.makeText(user_profile.this, "clicked", Toast.LENGTH_LONG).show();
}
});
}
}
共享偏好类别:
public class user_sharedpref_manager {
private static final String PROFILE_SHARED_PREF = "profile_Shared_pref";
private static final String PROFILE_FIRST_NAME = "profile_first_name";
private static final String PROFILE_LAST_NAME = "profile_first_name";
private static final String CAREER = "career";
private SharedPreferences sharedPreferences;
public user_sharedpref_manager(Context context) {
sharedPreferences = context.getSharedPreferences(PROFILE_SHARED_PREF, Context.MODE_PRIVATE);
}
public void save_user(user_sample userSample) {
if (userSample != null) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(PROFILE_FIRST_NAME, userSample.getFirstname());
editor.putString(PROFILE_LAST_NAME, userSample.getLastname());
editor.putInt(CAREER, userSample.getCareer());
editor.apply();
}
}
public user_sample get_user() {
user_sample userSample = new user_sample();
userSample.setFirstname(sharedPreferences.getString(PROFILE_FIRST_NAME, ""));
userSample.setLastname(sharedPreferences.getString(PROFILE_LAST_NAME, ""));
userSample.setCareer(sharedPreferences.getInt(CAREER, user_sample.gamer));
return userSample;
}
}
及其保存的共享首选项:
<map>
<int name="career" value="1" />
<string name="profile_first_name">gftfgtdrrdrt</string>
用户样本:
public class user_sample {
private String firstname ="";
private String lastname="";
public static final int gamer=0;
public static final int dancer=1;
public static final int soccer_player=2;
private int career=gamer;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getCareer() {
return career;
}
public void setCareer(int career) {
this.career = career;
}
答案 0 :(得分:3)
private static final String PROFILE_FIRST_NAME = "profile_first_name";
private static final String PROFILE_LAST_NAME = "profile_first_name";
两个键的值均为"profile_first_name"
。我猜PROFILE_LAST_NAME
应该等于"profile_last_name"
。