【问题标题】:Getting Shared Preferences with specific name使用特定名称获取共享首选项
【发布时间】:2018-06-30 09:44:06
【问题描述】:

我正在尝试将来自各种 EditText 和 Textview 框的多个变量存储在一个共享首选项中,我有一个特定的名称(因此没有可能的默认共享首选项)。从活动 C 保存并稍后检索工作正常。但是,如果我将它们保存在活动 D 的片段中并稍后尝试检索它,我会得到一个空白字段。甚至没有显示默认值。它一定与片段有关,但我找不到正确的语法来让它工作。这个答案(Shared Preferences in Fragment 等)帮不了我(因为它使用默认的共享首选项)。

这是提交共享首选项的活动 C 的 on create 方法的一部分

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_produktspezifikation);

        //region BUTTON DEKLARATION
        Button neuesWST = (Button) findViewById(R.id.btn_Neu_WST);
        neuesWST.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startActivity(new Intent(C_Produktspezifikation.this,WST_bearbeiten.class));
            }
        });

        Button Weiter = (Button) findViewById(R.id.btn_Produktspezifikation_weiter);
        Weiter.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                //region DATEN SPEICHERN
                //Variablen definieren, um die Daten aus den Feldern auszulesen
                TextView WstName = (TextView) findViewById(R.id.tv_WST);
                TextView Schnittweg = (TextView) findViewById(R.id.tv_Schnittweg_Eintrag);
                EditText Stueckzahl = (EditText) findViewById(R.id.et_Stuekzahl_Eintrag);
                EditText Losgroesse = (EditText) findViewById(R.id.et_Losgroesse_Eintrag);

                //Shared Preferences definieren
                SharedPreferences sharedpref = getSharedPreferences("Datenspeicher", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpref.edit();

                //Daten in benannter SharedPreference speichern
                editor.putString("wstname", WstName.getText().toString());
                editor.putString("schnittweg", Schnittweg.getText().toString());
                editor.putString("stueckzahl", Stueckzahl.getText().toString());
                editor.putString("losgroesse", Losgroesse.getText().toString());
                editor.commit();
                //endregion

                startActivity(new Intent(C_Produktspezifikation.this,D_Maschine.class));

            }
        });

除了Activity D的片段(另一个是相同的),其中更多的偏好是安全的:

public class Maschine_v1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       final  View rootView = inflater.inflate(R.layout.fragment_maschine_variante_1, container, false);

        //Variablen definieren, um die Daten aus den Feldern auszulesen
        TextView maschinenname_v1 = (TextView) rootView.findViewById(R.id.tv_m_v1_Maschine);
        TextView anschaffungswert_v1 = (TextView) rootView.findViewById(R.id.tv_m_v1_Anschaffungswert_Eintrag);
        TextView raumbedarf_v1 = (TextView) rootView.findViewById(R.id.tv_m_v1_Raumbedarf_Eintrag);
        TextView el_Anschlusswert_v1 = (TextView) rootView.findViewById(R.id.tv_m_v1_el_Anschlusswert_Eintrag);

        //Shared Preferences definieren
        SharedPreferences sharedpref = this.getActivity().getSharedPreferences("Datenspeicher", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpref.edit();

        //Daten in benannter SharedPreference speichern
        editor.putString("maschinenname_v1", maschinenname_v1.getText().toString());
        editor.putString("anschaffungswert_v1", anschaffungswert_v1.getText().toString());
        editor.putString("raumbedarf_v1", raumbedarf_v1.getText().toString());
        editor.putString("el_anschlusswert_v1", el_Anschlusswert_v1.getText().toString());
        editor.commit();

还有一个活动 E 的片段,其中应该检索来自活动 D 的共享首选项并且不显示: anschaffungswert_v1 来自活动 D(或者说它是片段)并且不起作用。

Schnittweg 来自活动 C 并且有效。

public class Werkzeug_v1 extends android.support.v4.app.Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.fragment_werkzeug_variante_1, container, false);

        TextView tv = (TextView) rootView.findViewById(R.id.tv_v1_Werkzeug_Abnutzung_Eintrag);
        TextView tv2 = (TextView) rootView.findViewById(R.id.tv_v1_Werkzeug_Schnittweg_Eintrag);
        SharedPreferences sharedPref = this.getActivity().getSharedPreferences("Datenspeicher", Context.MODE_PRIVATE);

        String test = sharedPref.getString("anschaffungswert_v1","N/A");
        String test2 = sharedPref.getString("schnittweg", "");
        tv.setText(test);
        tv2.setText(test2);

这是在活动 D 中保存后的Datenspeicher.xml(我的共享首选项文件):

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="anschaffungswert_v1"></string>
    <string name="maschinenname_v1"></string>
    <string name="schnittweg">1200</string>
    <string name="el_anschlusswert_v1"></string>
    <string name="losgroesse"></string>
    <string name="wstname">Werkstück 1</string>
    <string name="stueckzahl">33</string>
    <string name="raumbedarf_v1"></string>
</map>

【问题讨论】:

  • 抱歉代码垃圾邮件。我害怕错过一个我不知道的重要部分。而我完全忽略了,这些区域并没有崩溃。
  • 在Activity D的片段中保存后尝试检查sharedPreference文件。stackoverflow.com/a/45392716/6168272
  • 这些条目是空的,正如我所料(我编辑了我的问题并提供了 xml 文件)。我还是不明白为什么。

标签: android android-fragments sharedpreferences


【解决方案1】:

尝试使用 tiny DB...只需将 tinyDB.java 添加到您的项目中...https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo?files=1

【讨论】:

  • 它使用gson序列化对象和wtf,它甚至将png保存为base64,性能很糟糕。
猜你喜欢
  • 2012-09-03
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多