首页 > 解决方案 > shared prefrence does not save correctly

问题描述

Hi when i save informations the things i wrote in "last name" copy to "first name field" no matter what i write in first name and always "dancer radio button" saves. i think the problem is my activity i have no idea what i wrote =/

This is the activity :

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


}

shared preference class :

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;
    }
}

and its the saved shared preference :

<map>
<int name="career" value="1" />
<string name="profile_first_name">gftfgtdrrdrt</string>

user sample :

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;
}

标签: javaandroid

解决方案


private static final String PROFILE_FIRST_NAME = "profile_first_name";
private static final String PROFILE_LAST_NAME = "profile_first_name";

你的两个键都有 value "profile_first_name"。我猜PROFILE_LAST_NAME应该相等"profile_last_name"


推荐阅读