私はそれぞれ独自のSharedPreferencesキーの下に保存する必要がある5つの異なる文字列値を持っています。しかし、私はeditClass5Textに設定したものだけを保存しています。デフォルトの文字列で、Android Studio複数のSharedPreferencesを1つのキー値で保存しますか?
私は私のキー文字列を作ってみた最終および/またはStringにそれらを初期化しますが、それは
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);
}
であなたの変数を割り当てて、すでにここに答えるhttp://stackoverflow.com/questions/9054193/how-to-use-sharedpreferences-to-save-を参照してください。 1つ以上の値 – sasikumar
はい私はそれを見ましたが、複数の文字列を1つのキーに保存しようとしています。私は、しかし、複数の文字列に複数のキーを使用して、ここで説明:https://www.tutorialspoint.com/android/android_shared_preferences.htm – sfinger
これらのキーは正確にどこにあります:class1TextSAVEFILE etc? –