【发布时间】:2016-12-08 05:14:17
【问题描述】:
我有 5 个不同的字符串值需要保存在各自的 SharedPreferences 键下。但是,它们只保存为我为 editClass5Text 设置的内容。
我已经尝试将我的关键字符串设置为最终字符串和/或将它们初始化为字符串,但结果却是
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private static final String SAVEFILE = "saveFile";
private EditText editClass1Text, editClass2Text, editClass3Text, editClass4Text, editClass5Text;
private String class1TextSAVEFILE;
private String class2TextSAVEFILE;
private String class3TextSAVEFILE;
private String class4TextSAVEFILE;
private String class5TextSAVEFILE;
private Button button;
private View view;
public ClassSettings() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Class1.
*/
// TODO: Rename and change types and number of parameters
public static ClassSettings newInstance(String param1, String param2) {
ClassSettings fragment = new ClassSettings();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
//initialize the SharedPreferences and the editor in onCreate so it is the first thing the fragment does.
sp = this.getActivity().getSharedPreferences("classSettings", Context.MODE_PRIVATE);
editor = sp.edit();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//we initialize the view and set it to the inflater,
//so that we can return button, edit text, and set text in the inflater also.
view = inflater.inflate(R.layout.fragment_class_settings, container, false);
button = (Button) view.findViewById(R.id.buttonn);
button.setOnClickListener(this);
editClass1Text = (EditText) view.findViewById(R.id.class1);
editClass1Text.setText(sp.getString(null, class1TextSAVEFILE), TextView.BufferType.EDITABLE);
editClass2Text = (EditText) view.findViewById(R.id.class2);
editClass2Text.setText(sp.getString(null, class2TextSAVEFILE), TextView.BufferType.EDITABLE);
editClass3Text = (EditText) view.findViewById(R.id.class3);
editClass3Text.setText(sp.getString(null, class3TextSAVEFILE), TextView.BufferType.EDITABLE);
editClass4Text = (EditText) view.findViewById(R.id.class4);
editClass4Text.setText(sp.getString(null, class4TextSAVEFILE), TextView.BufferType.EDITABLE);
editClass5Text = (EditText) view.findViewById(R.id.class5);
editClass5Text.setText(sp.getString(null, class5TextSAVEFILE), TextView.BufferType.EDITABLE);
// return the view statement that includes the inflater, button edittext, and edittext.settext.
return view;
}
public void onClick(View v){
//change the EditText object to a String and save it to the sp class1
editor.putString(class1TextSAVEFILE, editClass1Text.getText().toString());
editor.putString(class2TextSAVEFILE, editClass2Text.getText().toString());
editor.putString(class3TextSAVEFILE, editClass3Text.getText().toString());
editor.putString(class4TextSAVEFILE, editClass4Text.getText().toString());
editor.putString(class5TextSAVEFILE, editClass5Text.getText().toString());
editor.commit();
//sets the text saved to the EditText object, and saves it as editable.
editClass1Text.setText(sp.getString(null, class1TextSAVEFILE), TextView.BufferType.EDITABLE);
editClass2Text.setText(sp.getString(null, class2TextSAVEFILE), TextView.BufferType.EDITABLE);
editClass3Text.setText(sp.getString(null, class3TextSAVEFILE), TextView.BufferType.EDITABLE);
editClass4Text.setText(sp.getString(null, class4TextSAVEFILE), TextView.BufferType.EDITABLE);
editClass5Text.setText(sp.getString(null, class5TextSAVEFILE), TextView.BufferType.EDITABLE);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
【问题讨论】:
-
是的,我看到了,但他们正试图将多个字符串保存到一个键中。但是,我对多个字符串使用多个键,在这里解释:tutorialspoint.com/android/android_shared_preferences.htm
-
这些键具体在哪里:class1TextSAVEFILE 等设置?
-
您是否为每个类初始化了这些 classTextSaveFile 值?
-
是的,我之前曾尝试初始化这些值,但它仍然无法正常工作。之前有人告诉我 SharedPreferences.getString() 中的第一个参数为空,但我用我初始化的相应字符串更改了它并且它起作用了。