Java 我如何才能使共享引用正常工作?
我很难让我的共享引用正常工作。我正在尝试将另一个活动中的文本框中的数据加载到文本视图中。我没有得到任何错误,但它一直返回默认值“nameKey” 此处是输入和保存数据的位置Java 我如何才能使共享引用正常工作?,java,android,sharedpreferences,Java,Android,Sharedpreferences,我很难让我的共享引用正常工作。我正在尝试将另一个活动中的文本框中的数据加载到文本视图中。我没有得到任何错误,但它一直返回默认值“nameKey” 此处是输入和保存数据的位置 public class ActivitySettings extends AppCompatActivity { SharedPreferences sharedPreferences; final String MyPREFERENCES = "MyPrefs"; final String Name = "nameK
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();
}
});
这里有一个不同的活动,我试图读入数据并将其设置为文本视图
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));
}
}
我认为最好创建一个类来处理
SharedReferences
,并将键作为静态字符串添加到其中,并使用插入和读取方法。这里有一个例子:
设置提供者类
公共类设置提供程序{
私有静态最终字符串设置\u提供程序\u NAME=“设置”;
公共静态最终字符串NAME=“NAME”
私人共享引用共享引用;
私有静态设置提供程序或实例;
私人共享引用共享引用;
专用设置提供程序(上下文){
if(上下文==null)
抛出新的RuntimeException(“上下文必须有效!”);
this.SharedReferences=context.getSharedReferences(设置\u提供者\u名称,0);
}
公共静态设置提供程序getInstance(上下文){
if(ourInstance==null){
ourInstance=新设置提供程序(上下文);
}
回归自然状态;
}
公共void集合名(字符串名){
返回此.SharedReferences.edit().setString(名称、名称).apply();
}
公共字符串getName(){
返回此.SharedReferences.getString(名称“”);
}
}
现在用于活动中
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u duel);
setingProvider.getInstance(this.setName(“名称”);
String name=SettingProvider.getInstance(this.getName();
}
像这样打电话
txtD1.setText(getPrefs("nameKey", this));
您的getPrefs()
函数应该如下所示
public String getPrefs(String n, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
return preferences.getString(n, "");
}
我认为问题在于您正在保存一个名为“Name”的共享首选项键,因此从字段
final String Name=“nameKey”代码>首选项键为“nameKey”
然后,当您尝试获取字符串时,您使用“name”作为查找键值,并且由于它不存在,它被设置为默认的“nameKey”,因此我认为您需要更改此txtD1.setText(getPrefs(“name”,this))
totxtD1.setText(getPrefs(Name,this))代码>