使用SharedPreferences在片段中显示MainActivity中保存的EditText

时间:2018-12-15 20:19:53

标签: android xml android-fragments sharedpreferences

我正在将EditText保存到SharedPreferences,但是在我的Fragment中看不到保存的文本。 有人可以告诉我我在做什么错,以及我对apk使用SharedPreferences的方式。

这是我在MainActivity中保存数据的代码。

public static final String Save_Search = "Save_Search";
public EditText searchPlugin;
public String textForSearch;


searchPlugin = findViewById(R.id.etSearch);


public void saveData() {
    SharedPreferences sharedPreferences = getSharedPreferences(Save_Search, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(editText, searchPlugin.getText().toString());  editor.putString(Save_Search, searchPlugin.getText().toString());
    Log.d("Test", searchPlugin.getText().toString());
//This is saving me the typed text.
}

public void updateView() {
        searchPlugin.setText(textForSearch);
    }

这是EditText的布局

<EditText android:id="@+id/etSearch" android:imeOptions="actionGo|flagNoExtractUi" style="@style/SearchEditText.MainSearch" />

这是片段

  public class FragmentHistory extends Fragment {
    View paramView;
    TextView textView;
    public FragmentHistory() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        paramView = inflater.inflate(R.layout.history_item, container, false);
        textView = paramView.findViewById(R.id.tvHistoryName);
        SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
        String text = pref.getString("Save_Search", "");
        textView.setText(text);
        return paramView;
    }


}

这是历史记录布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
    android:background="@drawable/selector_btn_home"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:id="@id/ivHistory" style="@style/ivHistory" />
    <TextView android:id="@id/tvHistoryName" android:layout_height="38dp" style="@style/TextViewQuery"/>
    <ImageView android:id="@id/imgViewSetQuery" style="@style/ImageViewSetQuery" />
    <TextView android:textSize="@dimen/search_font" android:textColor="@android:color/white"
        android:gravity="center" android:id="@id/btnDelete" android:background="@color/red" android:paddingLeft="5.0dip"
        android:paddingRight="5.0dip" android:visibility="gone" android:longClickable="false"
        android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/Delete" />
</LinearLayout>

3 个答案:

答案 0 :(得分:4)

您正在尝试通过“ Save_Search”键保存数据,但是您正在保存数据 在“ editText”键下

替换此行:

 editor.putString(editText, searchPlugin.getText().toString());

使用此行

editor.putString(Save_Search, searchPlugin.getText().toString());

答案 1 :(得分:1)

您可以轻松使用PowerPreference

https://github.com/AliEsaAssadi/Android-Power-Preference

保存数据

public void saveData() {
  PowerPreference.defult().put("Save_Search",searchPlugin.getText().toString());   
}

获取数据

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

  String text = PowerPreference.defult().getString("Save_Search");
  textView.setText(text);

}

答案 2 :(得分:0)

替换:

SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);

使用:

SharedPreferences preferences = this.getActivity().getSharedPreferences("Save_Search", Context.MODE_PRIVATE);

在您的片段中

相关问题