Я сохраняю EditText в SharedPreferences, но я не могу увидеть в своем фрагменте сохраненный текст.Может кто-нибудь сказать мне, что я делаю неправильно, и, кстати, я использую SharedPreferences для моего apk.
Это мой код сохранения данных в 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>