Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在共享首选项android中存储字符串时如何写入而不是覆盖_Java_Android_String_Storage_Android Sharedpreferences - Fatal编程技术网

Java 在共享首选项android中存储字符串时如何写入而不是覆盖

Java 在共享首选项android中存储字符串时如何写入而不是覆盖,java,android,string,storage,android-sharedpreferences,Java,Android,String,Storage,Android Sharedpreferences,存储字符串: Encryption encryption = Encryption.getDefault("Key", "Salt", new byte[16]); String encryptedP = encryption.encryptOrNull(Password); String encryptedU = encryption.encryptOrNull(Username); SharedP

存储字符串:

Encryption encryption = Encryption.getDefault("Key", "Salt", new byte[16]);
                String encryptedP = encryption.encryptOrNull(Password);
                String encryptedU = encryption.encryptOrNull(Username);    
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            Editor edit = prefs.edit();
            edit.putString("Password", encryptedP);
            edit.putString("Username", encryptedU);
            edit.commit(); 
Encryption encryption = Encryption.getDefault("Key", "Salt", new byte[16]);
    String encryptedP = encryption.encryptOrNull(Password);
    String encryptedU = encryption.encryptOrNull(Username);
    SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
    String UsernamePref = sPrefs.getString("Username", "");
    String PasswordPref = sPrefs.getString("Password", "");
    if(encryptedU.equals(UsernamePref) &&  (encryptedP.equals(PasswordPref))) {  if((NewPassword.matches (ConfirmPassword))) {
                Editor edit = sPrefs.edit();
                edit.putString("Password", encryptedN);
                edit.commit();          
检索字符串:

Encryption encryption = Encryption.getDefault("Key", "Salt", new byte[16]);
                String encryptedP = encryption.encryptOrNull(Password);
                String encryptedU = encryption.encryptOrNull(Username);    
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            Editor edit = prefs.edit();
            edit.putString("Password", encryptedP);
            edit.putString("Username", encryptedU);
            edit.commit(); 
Encryption encryption = Encryption.getDefault("Key", "Salt", new byte[16]);
    String encryptedP = encryption.encryptOrNull(Password);
    String encryptedU = encryption.encryptOrNull(Username);
    SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
    String UsernamePref = sPrefs.getString("Username", "");
    String PasswordPref = sPrefs.getString("Password", "");
    if(encryptedU.equals(UsernamePref) &&  (encryptedP.equals(PasswordPref))) {  if((NewPassword.matches (ConfirmPassword))) {
                Editor edit = sPrefs.edit();
                edit.putString("Password", encryptedN);
                edit.commit();          
检索时,除非检索最后存储的字符串,否则它不会成功,因为它不会查看所有行。只有第一行,我如何更改它,以便它在关闭文件之前扫描每一行? 谢谢

编辑:好吧,我只是意识到它每次都会被覆盖,而不是将它存储到新行,这就是为什么它不能检索“旧字符串”,新问题。如何在每次创建新用户时存储和不覆盖?

此“错误”是因为您没有正确存储首选项:

您需要在编辑器中提交首选项

做:


我认为如果你使用数据库会更好。使用共享pref存储大量数据需要大量复杂的代码

每次需要存储大量相同类型的数据项时(在本例中为密码和用户名),都应该使用数据库而不是共享pref

我知道在android中设置SQLite数据库非常烦人,需要大量代码。所以为了简化一些,我推荐图书馆。它是一个对象关系映射(ORM)库,可以简化您使用SQLite数据库的体验

基本上,通过创建扩展
SugarRecord

public class User extends SugarRecord<User> {
    public String username;
    public String password;

    public User () {} // parameterless constructor is required
    public User (String username, String password) {
        this.username = username;
        this.password = password;
    }
}
您可以使用
listAll
find
findById
等方法来查询数据库!支持一对多关系


您可以在网站上找到更多信息。

要将记录存储在
SharedReferences
中,您可以修改每个用户的首选项键。它将防止覆盖这些值

比如说

String getFormattedName(String username, String value) {
    return String.format("%s_%s", username, value);
}
然后您可以像这样存储记录:

edit.putString(getFormattedName(encryptedU, "Password"), encryptedP);
edit.putString(getFormattedName(encryptedU, "Username"), encryptedU);
并检索:

String UsernamePref = sPrefs.getString(getFormattedName(encryptedU, "Username"), "");
String PasswordPref = sPrefs.getString(getFormattedName(encryptedU, "Password"), "");

因此,密钥将类似于
123123\u Username
。当然,您可以根据需要修改模式。

很抱歉,我在代码中遗漏了这一点,我认为这很明显,因为它在登录过程中起作用,我的错!抱歉,我会编辑。有什么理由不使用SQLite吗?我正在学习使用内部存储。尝试一下,它也是“内部存储”的一部分。那么,按照我的要求做是不可能的吗?不能说是不可能的,只是你会发现这样做很愚蠢。很好,你打败了我。=)
package com.example.preferences;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;

public class DumbPref {
    private SharedPreferences mPreferences;

    public DumbPref(@NonNull Context context, @NonNull String name) {
        mPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    @SuppressLint("CommitPrefEdits")
    public void addUser(@NonNull String name, @NonNull String password) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putBoolean(makeKey(name, password), true);
        editor.commit();
    }

    public boolean isAuthenticated(String name, String password) {
        return mPreferences.getBoolean(makeKey(name, password), false);
    }

    private static String makeKey(String name, String password) {
        return name + "_" + password;
    }
}