【问题标题】:Showing values which is put in sharedpreferences are not showing in textView [duplicate]显示放入 sharedpreferences 中的值未显示在 textView [重复]
【发布时间】:2016-08-01 04:53:15
【问题描述】:

没有从 对 textview 的共享偏好 中获得价值。 查看代码。 有没有错误。 以及下面两行代码有什么区别...

sharedPreferences = getSharedPreferences("Mypreferences",Context.MODE_PRIVATE);

sharedPreferences=PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

以下是我的代码行..

public class MainActivity extends AppCompatActivity {
  EditText et_name, et_phon, et_email, et_city;
  Button button_submit, button_show;
  SharedPreferences sharedPreferences;
  SharedPreferences.Editor editor;
  TextView tv_name,tv_phone,tv_email,tv_city;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initComponents();
    sharedPreferences = getSharedPreferences("Mypreferences", Context.MODE_PRIVATE);

    button_submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name, phon, email, city;
            name = et_name.getText().toString().trim();
            phon = et_phon.getText().toString().trim();
            email = et_email.getText().toString().trim();
            city = et_city.getText().toString().trim();

            editor = sharedPreferences.edit();
            editor.putString("NameKey", name);
            editor.putString("phoneKey", phon);
            editor.putString("emailKey", email);
            editor.putString("cityKey", city);
            editor.apply();
            Toast.makeText(MainActivity.this, "Submitted", Toast.LENGTH_SHORT).show();


        }
    });
    button_show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            String n = sharedPreferences.getString("NameKey", null);
            String p = sharedPreferences.getString("phoneKey", null);
            String e = sharedPreferences.getString("emailKey", null);
            String c = sharedPreferences.getString("cityKey", null);

            tv_name.setText(n);
            tv_phone.setText(p);
            tv_email.setText(e);
            tv_city.setText(c);

            Toast.makeText(MainActivity.this, "Show button clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

private void initComponents() {

    et_name = (EditText) findViewById(R.id.et_name_main);
    et_phon = (EditText) findViewById(R.id.et_phone_main);
    et_email = (EditText) findViewById(R.id.et_email_main);
    et_city = (EditText) findViewById(R.id.et_city_main);
    tv_name = (TextView) findViewById(R.id.tv_name_main);
    tv_phone = (TextView) findViewById(R.id.tv_phone_main);
    tv_email = (TextView) findViewById(R.id.tv_email_main);
    tv_city = (TextView) findViewById(R.id.tv_city_main);
    button_submit = (Button) findViewById(R.id.btn_submit_main);
    button_show = (Button) findViewById(R.id.btn_view_main);

}
}

感谢任何类型的帮助。

【问题讨论】:

  • 这只是创建了两个不同的 sharedPreferences.xml 。在您的应用程序目录中,默认名称为 shared_preferences,在您的情况下,另一个名称为“mypreferences”。 MODE_PRIVATE 表示,没有其他应用可以进入。
  • 您保存在不同的 SharedPreferences 实例中并在默认实例中检索。在放置和检索时使用getSharedPreferences("Mypreferences", Context.MODE_PRIVATE) 或默认一个。
  • 谢谢。你们澄清了我的观点,我想在那个论点中了解模式。

标签: android sharedpreferences android-sqlite android-sharedpreferences


【解决方案1】:

问题是,您正在创建一个名为 "Mypreferences"sharedPref 来存储值并尝试从 default sharedPreferences 中检索不包含您拥有的值的值放。

换行

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

SharedPreferences sharedPreferences = getSharedPreferences("Mypreferences", Context.MODE_PRIVATE); 

【讨论】:

  • 谢谢!小概率...
【解决方案2】:

您正在您的应用目录中创建两个sharedPreferences xml files。默认一个是标准的preferences xml,另一个是你用

创建的
sharedPreferences = getSharedPreferences("Mypreferences", Context.MODE_PRIVATE);

会将您的首选项存储在 Mypreferences.xml 中。 MODE_PRIVATE 表示,这个preferences 仅适用于您的应用程序(但在有根设备上,每个人都可以访问)。

如果您想访问已保存的 sharedPreferences,您必须使用与保存它们相同的首选项。这意味着您必须在访问相同密钥的任何地方使用:

sharedPreferences = getSharedPreferences("Mypreferences", Context.MODE_PRIVATE);

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

两者都不是。在您的button_show click method 中,您不必创建新的sharedPreferences object,只需删除此行:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

因为您已经生成了一个全局sharedPreferences object,并且可以在您的class 中的任何地方访问它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多