如何使我的共享首选项正常工作?

时间:2019-05-22 18:33:21

标签: java android sharedpreferences

我无法让我的共享首选项正常工作。我正在尝试将另一个活动中的文本框中的数据加载到textview中。我没有收到任何错误,但是它一直返回“ nameKey”的默认值。

在这里输入和保存数据

public class ActivitySettings extends AppCompatActivity {

 SharedPreferences sharedPreferences;
final String MyPREFERENCES = "MyPrefs";
final String Name = "nameKey";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final EditText enterName1 = (EditText) findViewById(R.id.enterName1); 
    final EditText enterName2 = (EditText) findViewById(R.id.enterName2); 

    Button btnOK = (Button) findViewById(R.id.btnOK);
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

    btnOK.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String n = enterName1.getText().toString();

    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(Name, n);
    editor.commit();

        }
    });

这是另一种活动,我正在尝试读取数据并将其设置为textview

public class ActivityDuel extends AppCompatActivity {

public String getPrefs(String n, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    return preferences.getString(n, "nameKey");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_duel);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final TextView txtD1 = (TextView) findViewById(R.id.txtD1);
    txtD1.setText(getPrefs("name", this));
}
}

3 个答案:

答案 0 :(得分:2)

像这样打电话

 txtD1.setText(getPrefs("nameKey", this));

您的getPrefs()函数应如下所示

public String getPrefs(String n, Context context) {
  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  return preferences.getString(n, "");
}

答案 1 :(得分:1)

我认为最好创建一个用于处理SharedPreferences的类,并将键作为静态字符串添加到该类中,并使用用于插入和读取的方法。这里是一个例子:

SettingProvider.class

public class SettingProvider {

    private static final String SETTING_PROVIDER_NAME = "setting";
    public static final String NAME = "name"
    private SharedPreferences sharedPreferences;

    private static SettingProvider ourInstance;

    private SharedPreferences sharedPreferences;

    private SettingProvider(Context context) {
        if (context == null)
            throw new RuntimeException("context must be valid!");
        this.sharedPreferences = context.getSharedPreferences(SETTING_PROVIDER_NAME, 0);
    }

    public static SettingProvider getInstance(Context context) {
        if (ourInstance == null) {
            ourInstance = new SettingProvider(context);
        }
        return ourInstance;
    }
    public void setName(String name) {
        return this.sharedPreferences.edit().setString(NAME, name).apply();
    }

    public String getName() {
        return this.sharedPreferences.getString(NAME, "");
    }
}

现在可以在活动中使用


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_duel);
    SettingProvider.getInstance(this).setName("the name");
    String name = SettingProvider.getInstance(this).getName();
}

答案 2 :(得分:0)

我认为问题在于您正在保存共享的“姓名”偏好设置键,因此在字段final String Name = "nameKey";中,偏好设置键为“ nameKey”

然后,当您尝试获取字符串时,您将使用“名称”作为查找键值,由于它不存在,因此将其设置为默认的“ nameKey”,因此我认为您需要更改此{{ 1}}至txtD1.setText(getPrefs("name", this));