在配置文件中编辑显示名称时如何存储重复共享首选项

How to store repeat share preference when edit display name in profile

我想在用户更改个人资料中的显示名称时再次存储共享首选项

现在,数据库中的数据已更改,但我不知道更改共享首选项


请帮助我。

代码:

    private static final String PREFERENCE_KEY = "pref_user";
    private static final String DISPLAY_KEY = "user_display_name";
    SharedPreferences sh_pref;
    SharedPreferences.Editor sh_edit;

    UserPreference setting;
    private int user_id;
    private String getuser_id;
    private String user_display_name;

    EditText EDTEditdisplay;


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



        // custom my action bar
        ActionBar actionbar = getActionBar();
        actionbar .setTitle("CHANGE DISPLAY NAME");
        // back navigation
        actionbar.setDisplayHomeAsUpEnabled(true);
        // hide icon app
        getActionBar().setDisplayShowHomeEnabled(false);

        setting = new UserPreference(ChangeDisplayNameActivity.this);       
        user_id = setting.getUserID();  
        getuser_id = String.valueOf(user_id);

        Button BTNsaveDisplayname = (Button) findViewById(R.id.BTNsaveDisplayname);
        BTNsaveDisplayname.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                UpdateDisplayName();
            }
        });


    }//end onCreate


    public boolean UpdateDisplayName() {

        EDTEditdisplay = (EditText) findViewById(R.id.EDTEditdisplay);

        // Dialog alert
        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        // Check Displayname
        if(EDTEditdisplay.getText().length() == 0)
        {
            alert.setMessage("Please input new display name");
            alert.show();
            EDTEditdisplay.requestFocus();
            return false;
        }

        // gettext from edittext
        user_display_name = EDTEditdisplay.getText().toString();

        List<NameValuePair> update_display = new ArrayList<NameValuePair>();
        update_display.add(new BasicNameValuePair("user_id", getuser_id));
        update_display.add(new BasicNameValuePair("user_display_name", user_display_name));

        JSONParser jspaser = new JSONParser();
        String url = "http://su13540254.2th.asia/Hello_Oppa/update_displayname.php";
        String dataJSON = jspaser.makeHttpRequest(url,JSONParser.methodPost, update_display);


        Toast.makeText(getApplicationContext(),
                 "Change display name, Complete", Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(ChangeDisplayNameActivity.this, MainActivity.class);
        startActivity(intent);

        return true;

    }//end UpdateDisplayName

在数据库中保存显示名称后,再次将代码放在您使用相同键保存共享首选项的位置,它将替换您在共享首选项中的显示名称。例如。 要保存:

SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
mPreferences.edit().putString("DISPLAY_NAME",user_display_name).commit();

检索:

SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String display_name = mPreferences.getString("DISPLAY_NAME","Default Name");