1

I am trying to save a string 'reftext' to the first element of an array 'savedabc[]' using SharedPreferences. My variables are declared as below.

private EditText reftext;
private static SharedPreferences savedabc;
private static EditText[] savedabc;

Here is the 'Save' method:

    public void onSaveClick(View view) {
    SharedPreferences savedabc = getSharedPreferences("SavedABC", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = savedabc.edit();
    editor.putString(savedabc[0], reftext);
    editor.commit();
}

However, this gives me the following error: "The method putString(String, String) in the type SharedPreferences.Editor is not applicable for the arguments (EditText, EditText)".

Could anyone help with this please?

Many thanks.

petehallw
  • 1,014
  • 6
  • 21
  • 49
  • 2 variables with the same name... not good. Also, you are passing in 2 `EditTexts` when what it wants is 2 `Strings`. – takendarkk Jan 11 '14 at 15:58
  • Ah yes I've changed the variable names, thanks. So what would be a better way of doing this using editor? – petehallw Jan 11 '14 at 16:04
  • You need to extract the strings from your `EditTexts` using something along the lines of `editText.getText()`. That will give you a `String` which you can put into `SharedPreferences`. – takendarkk Jan 11 '14 at 16:06
  • Ok I can see that working for the 'reftext' string but for the array as well? – petehallw Jan 11 '14 at 17:32
  • This can be help to you : http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – ZG-RG Jan 11 '14 at 19:59

0 Answers0