简体   繁体   English

在共享首选项Android中保存用户ID

[英]Saving User ID in Shared Preferences Android

I just asked a question about this but I want to go at it a different way. 我刚问了一个关于这个的问题,但我想以不同的方式去做。 When a user edits his/her profile and hits the save button, I want to be able to generate a random number using UUID. 当用户编辑他/她的个人资料并点击保存按钮时,我希望能够使用UUID生成随机数。 I want this ID to stay the same if the user goes back and edits their profile a second time (if they press 'save' again I want to retain the ID that was generated the first time they pressed 'save'). 我希望这个ID保持不变,如果用户返回并再次编辑他们的个人资料(如果他们再次按'保存'我想保留他们第一次按'保存'时生成的ID)。 I have the following code working to save other data, but I'm not sure how to include a check that can find out if an ID has already been generated. 我有以下代码用于保存其他数据,但我不知道如何包含一个检查,可以找出是否已生成一个ID。 Here is my code: 这是我的代码:

public void save(View view) {
    String firstnameText = firstname.getText().toString();
    String lastnameText = lastname.getText().toString();
    String phoneText = phone.getText().toString();
    String cityText = city.getText().toString();
    String zipText = zip.getText().toString();
    String uuid = UUID.randomUUID().toString(); //Generate random ID but I 
                                                 think this would generate a 
                                                 new ID each time the data is     
                                                 saved

    if (firstnameText != null)
        PreferenceConnector.writeString(this, PreferenceConnector.FIRSTNAME,
                firstnameText);
    if (lastnameText != null)
        PreferenceConnector.writeString(this, PreferenceConnector.LASTNAME,
                lastnameText);

    if (phoneText != null && !phoneText.equals(""))
        PreferenceConnector.writeLong(this, PreferenceConnector.PHONE,
                Long.parseLong(phoneText));

    if (cityText != null)
        PreferenceConnector.writeString(this, PreferenceConnector.CITY,
                cityText);

    if (zipText != null && !zipText.equals(""))
        PreferenceConnector.writeInteger(this, PreferenceConnector.ZIP,
                Integer.parseInt(zipText));

    if (uuid != null) //what next?

    startActivity(new Intent(PreferencesActivity.this, Main.class));                        
}

You can set a Boolean SharedPreference that is initialy set to false and then set to true the foirst time an ID is generated, then you check this boolean before generating and ID and only generate if it is false so basicly 您可以设置一个初始设置为false的布尔型SharedPreference,然后将生成ID的第一次设置为true,然后在生成和ID之前检查此布尔值,并仅在生成ID时生成它

//get idHasBeenGenerated from prefs with default false
if(!idHasBeenGenerated)
{
//generate ID
//change value of idHasBeenGenerated to true and save in shared prefs.
}

Edit: 编辑:

SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean idHasBeenGenerated = prefs.getBoolean("idgenerated", false);

if(!idHasBeenGenerated){
    String uuid = UUID.randomUUID().toString();

//do your thing with PreferenceConnector
    Editor editor=prefs.edit();
    editor.putBoolean("idgenerated", true);
    editor.commit();
}else{          
    //Do nothing ID has already been generated
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM