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

时间:2015-03-14 05:06:34

标签: java android sharedpreferences

我想在用户更改个人资料

中的显示名称时再次存储共享首选项

现在,数据库中的数据已发生变化,但我不知道如何更改共享偏好


请帮助我。

代码:

    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

1 个答案:

答案 0 :(得分:1)

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

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");