0

I have 5 different String values that need to save under each of their own SharedPreferences key. However, they are only saving as what I set for editClass5Text.

I've tried making my key Strings final and/or initializing them to a String, but then it

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);
}
sfinger
  • 45
  • 5
  • 1
    already answer here refer http://stackoverflow.com/questions/9054193/how-to-use-sharedpreferences-to-save-more-than-one-values – sasikumar Dec 08 '16 at 05:17
  • yes I saw that but they are trying to save multiple strings to one key. I, however, am using multiple keys for multiple Strings, explained here: https://www.tutorialspoint.com/android/android_shared_preferences.htm – sfinger Dec 08 '16 at 05:22
  • where exactly are these keys: class1TextSAVEFILE etc set? – Suraj Rao Dec 08 '16 at 05:28
  • Have you initialised those classTextSaveFile values for each ? – Vinodh Dec 08 '16 at 05:29
  • Yes I had previously tried to initialize the values and it still wasnt working. I was previously told that the first parameter in SharedPreferences.getString() was to be null, but instead i changed it with the corresponding strings i initialized and it worked. – sfinger Dec 08 '16 at 05:38
  • Man You are writing String values with one key... your key is null so you are writing the values editClass5Text.getText().toString()) into null key.. so you will get always the last writed values.. – Mr.Popular Dec 08 '16 at 07:24

1 Answers1

0

You didn't assign any string to your variables, By default String is assigned with null.That is the reason you are able to save editClass5Text value.

null key , editClass4Text value (replaced by editClass3Text)

null key, editClass5Text value replaced by editClass4Text)

Assign your variables in the following way

private String class1TextSAVEFILE = "k1";

private String class2TextSAVEFILE = "k2";

private String class3TextSAVEFILE = "k3";

private String class4TextSAVEFILE = "k4";

private String class5TextSAVEFILE = "k5";
Dileep Patel
  • 1,988
  • 2
  • 12
  • 26
Anil Raavi
  • 131
  • 1
  • 10