Saving several EditTexts in one activity efficiently with SharedPreferences











up vote
0
down vote

favorite












I found answers to similar questions but none worked for me.

Q1: Saving several EditText after program closes

Q2: Save latest text from Edittext and restore it after onDestroy



In my case, we're talking about ~50 EditTexts. With question Q2 I figured how to save one single EditText but as soon as I copy the code for different EditTexts (and use different parameters) when restarting every EditText shows the same word.



public static final String LAST_TEXT1 = "";
public static final String LAST_TEXT2 = "";
public EditText myEditText1, myEditText2;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);

myEditText1= (EditText)findViewById(R.id.myEditText1);
final SharedPreferences pref1= PreferenceManager.getDefaultSharedPreferences(this);
myEditText1.setText(pref.getString(LAST_TEXT1, ""));
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

pref.edit().putString(LAST_TEXT1, s.toString()).commit();


}
});

myEditText2= (EditText)findViewById(R.id.myEditText2);
final SharedPreferences pref2= PreferenceManager.getDefaultSharedPreferences(this);
myEditText2.setText(pref.getString(LAST_TEXT2, ""));
myEditText2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

pref.edit().putString(LAST_TEXT2, s.toString()).commit();


}
});
}


Basically, this several times with all the parameters changed. (pref, myEditText,LAST_TEXT) But all the different EditTexts (like myEditText1 and myEditText2) would on the restart then show the same word.



Is there any efficient way to save all these contents?

Would there be a better way to save Strings in a table (I'm a beginner)
If there is no "efficient" way to save all these EditTexts how can I change my code so that it would save the data right?



Thanks in advance.










share|improve this question
























  • are the edittexts inside and recyclerView or gridView?
    – Kaveri
    Nov 11 at 16:38















up vote
0
down vote

favorite












I found answers to similar questions but none worked for me.

Q1: Saving several EditText after program closes

Q2: Save latest text from Edittext and restore it after onDestroy



In my case, we're talking about ~50 EditTexts. With question Q2 I figured how to save one single EditText but as soon as I copy the code for different EditTexts (and use different parameters) when restarting every EditText shows the same word.



public static final String LAST_TEXT1 = "";
public static final String LAST_TEXT2 = "";
public EditText myEditText1, myEditText2;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);

myEditText1= (EditText)findViewById(R.id.myEditText1);
final SharedPreferences pref1= PreferenceManager.getDefaultSharedPreferences(this);
myEditText1.setText(pref.getString(LAST_TEXT1, ""));
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

pref.edit().putString(LAST_TEXT1, s.toString()).commit();


}
});

myEditText2= (EditText)findViewById(R.id.myEditText2);
final SharedPreferences pref2= PreferenceManager.getDefaultSharedPreferences(this);
myEditText2.setText(pref.getString(LAST_TEXT2, ""));
myEditText2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

pref.edit().putString(LAST_TEXT2, s.toString()).commit();


}
});
}


Basically, this several times with all the parameters changed. (pref, myEditText,LAST_TEXT) But all the different EditTexts (like myEditText1 and myEditText2) would on the restart then show the same word.



Is there any efficient way to save all these contents?

Would there be a better way to save Strings in a table (I'm a beginner)
If there is no "efficient" way to save all these EditTexts how can I change my code so that it would save the data right?



Thanks in advance.










share|improve this question
























  • are the edittexts inside and recyclerView or gridView?
    – Kaveri
    Nov 11 at 16:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I found answers to similar questions but none worked for me.

Q1: Saving several EditText after program closes

Q2: Save latest text from Edittext and restore it after onDestroy



In my case, we're talking about ~50 EditTexts. With question Q2 I figured how to save one single EditText but as soon as I copy the code for different EditTexts (and use different parameters) when restarting every EditText shows the same word.



public static final String LAST_TEXT1 = "";
public static final String LAST_TEXT2 = "";
public EditText myEditText1, myEditText2;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);

myEditText1= (EditText)findViewById(R.id.myEditText1);
final SharedPreferences pref1= PreferenceManager.getDefaultSharedPreferences(this);
myEditText1.setText(pref.getString(LAST_TEXT1, ""));
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

pref.edit().putString(LAST_TEXT1, s.toString()).commit();


}
});

myEditText2= (EditText)findViewById(R.id.myEditText2);
final SharedPreferences pref2= PreferenceManager.getDefaultSharedPreferences(this);
myEditText2.setText(pref.getString(LAST_TEXT2, ""));
myEditText2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

pref.edit().putString(LAST_TEXT2, s.toString()).commit();


}
});
}


Basically, this several times with all the parameters changed. (pref, myEditText,LAST_TEXT) But all the different EditTexts (like myEditText1 and myEditText2) would on the restart then show the same word.



Is there any efficient way to save all these contents?

Would there be a better way to save Strings in a table (I'm a beginner)
If there is no "efficient" way to save all these EditTexts how can I change my code so that it would save the data right?



Thanks in advance.










share|improve this question















I found answers to similar questions but none worked for me.

Q1: Saving several EditText after program closes

Q2: Save latest text from Edittext and restore it after onDestroy



In my case, we're talking about ~50 EditTexts. With question Q2 I figured how to save one single EditText but as soon as I copy the code for different EditTexts (and use different parameters) when restarting every EditText shows the same word.



public static final String LAST_TEXT1 = "";
public static final String LAST_TEXT2 = "";
public EditText myEditText1, myEditText2;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);

myEditText1= (EditText)findViewById(R.id.myEditText1);
final SharedPreferences pref1= PreferenceManager.getDefaultSharedPreferences(this);
myEditText1.setText(pref.getString(LAST_TEXT1, ""));
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

pref.edit().putString(LAST_TEXT1, s.toString()).commit();


}
});

myEditText2= (EditText)findViewById(R.id.myEditText2);
final SharedPreferences pref2= PreferenceManager.getDefaultSharedPreferences(this);
myEditText2.setText(pref.getString(LAST_TEXT2, ""));
myEditText2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

pref.edit().putString(LAST_TEXT2, s.toString()).commit();


}
});
}


Basically, this several times with all the parameters changed. (pref, myEditText,LAST_TEXT) But all the different EditTexts (like myEditText1 and myEditText2) would on the restart then show the same word.



Is there any efficient way to save all these contents?

Would there be a better way to save Strings in a table (I'm a beginner)
If there is no "efficient" way to save all these EditTexts how can I change my code so that it would save the data right?



Thanks in advance.







android android-studio android-edittext sharedpreferences






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 16:23









Mostafa Arian Nejad

507416




507416










asked Nov 11 at 16:17









timoruether

147




147












  • are the edittexts inside and recyclerView or gridView?
    – Kaveri
    Nov 11 at 16:38


















  • are the edittexts inside and recyclerView or gridView?
    – Kaveri
    Nov 11 at 16:38
















are the edittexts inside and recyclerView or gridView?
– Kaveri
Nov 11 at 16:38




are the edittexts inside and recyclerView or gridView?
– Kaveri
Nov 11 at 16:38












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Based on your code, you are using same key for all EditText and that's what it's giving you back:



public static final String LAST_TEXT1 = "";
public static final String LAST_TEXT2 = "";


Here LAST_TEXT1 and LAST_TEXT2 are equals. SharedPreferences are key/value based storage option. If you want to save different values for all your editexts, use different keys. And I'd recommend, not empty Strings.



You can also use one key and store an object containing all your edittexts values structured the way you want, the way you should be able to retrieve data easily, for example using JSONArray and stringify it.



The content of EditText2 will replace the content of EditText1 in your preferences since you are using the same key.



And when you try to retrieve your content, the associated value of "" will be shown. LAST_TEXT1 and LAST_TEXT2 are just variables here. Keys are what is contained within the variable.



I hope this will help.






share|improve this answer

















  • 1




    You're right, the key is the error here. I will take a look at JSONArray then, thanks for the hint! For now editing the keys will do, so thanks.
    – timoruether
    Nov 11 at 17:43










  • Great! Do not hesitate to ask more informations or post other questions in case you have an issue
    – gratien asimbahwe
    Nov 11 at 17:46


















up vote
0
down vote













1.SharedPreference saves data in key-value pair. You save a data for a key and when needed, use that same key to recover the value, even if the app is closed.



In your case, you are using two variables for storing string-keys, named, LAST_TEXT1 and LAST_TEXT2. But sadly, both are carrying the same value, "" or empty string. As both keys are technically the same, you are getting the same result. Define Those two keys like this to get different results:



public static final String LAST_TEXT1 = "LAST_TEXT1";
public static final String LAST_TEXT2 = "LAST_TEXT2";


2.SharedPreference is used for simple and short key-values. If your values are longer than few words, I suggest using Sqlite Database or similar long time data persisting methods.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250666%2fsaving-several-edittexts-in-one-activity-efficiently-with-sharedpreferences%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Based on your code, you are using same key for all EditText and that's what it's giving you back:



    public static final String LAST_TEXT1 = "";
    public static final String LAST_TEXT2 = "";


    Here LAST_TEXT1 and LAST_TEXT2 are equals. SharedPreferences are key/value based storage option. If you want to save different values for all your editexts, use different keys. And I'd recommend, not empty Strings.



    You can also use one key and store an object containing all your edittexts values structured the way you want, the way you should be able to retrieve data easily, for example using JSONArray and stringify it.



    The content of EditText2 will replace the content of EditText1 in your preferences since you are using the same key.



    And when you try to retrieve your content, the associated value of "" will be shown. LAST_TEXT1 and LAST_TEXT2 are just variables here. Keys are what is contained within the variable.



    I hope this will help.






    share|improve this answer

















    • 1




      You're right, the key is the error here. I will take a look at JSONArray then, thanks for the hint! For now editing the keys will do, so thanks.
      – timoruether
      Nov 11 at 17:43










    • Great! Do not hesitate to ask more informations or post other questions in case you have an issue
      – gratien asimbahwe
      Nov 11 at 17:46















    up vote
    1
    down vote



    accepted










    Based on your code, you are using same key for all EditText and that's what it's giving you back:



    public static final String LAST_TEXT1 = "";
    public static final String LAST_TEXT2 = "";


    Here LAST_TEXT1 and LAST_TEXT2 are equals. SharedPreferences are key/value based storage option. If you want to save different values for all your editexts, use different keys. And I'd recommend, not empty Strings.



    You can also use one key and store an object containing all your edittexts values structured the way you want, the way you should be able to retrieve data easily, for example using JSONArray and stringify it.



    The content of EditText2 will replace the content of EditText1 in your preferences since you are using the same key.



    And when you try to retrieve your content, the associated value of "" will be shown. LAST_TEXT1 and LAST_TEXT2 are just variables here. Keys are what is contained within the variable.



    I hope this will help.






    share|improve this answer

















    • 1




      You're right, the key is the error here. I will take a look at JSONArray then, thanks for the hint! For now editing the keys will do, so thanks.
      – timoruether
      Nov 11 at 17:43










    • Great! Do not hesitate to ask more informations or post other questions in case you have an issue
      – gratien asimbahwe
      Nov 11 at 17:46













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Based on your code, you are using same key for all EditText and that's what it's giving you back:



    public static final String LAST_TEXT1 = "";
    public static final String LAST_TEXT2 = "";


    Here LAST_TEXT1 and LAST_TEXT2 are equals. SharedPreferences are key/value based storage option. If you want to save different values for all your editexts, use different keys. And I'd recommend, not empty Strings.



    You can also use one key and store an object containing all your edittexts values structured the way you want, the way you should be able to retrieve data easily, for example using JSONArray and stringify it.



    The content of EditText2 will replace the content of EditText1 in your preferences since you are using the same key.



    And when you try to retrieve your content, the associated value of "" will be shown. LAST_TEXT1 and LAST_TEXT2 are just variables here. Keys are what is contained within the variable.



    I hope this will help.






    share|improve this answer












    Based on your code, you are using same key for all EditText and that's what it's giving you back:



    public static final String LAST_TEXT1 = "";
    public static final String LAST_TEXT2 = "";


    Here LAST_TEXT1 and LAST_TEXT2 are equals. SharedPreferences are key/value based storage option. If you want to save different values for all your editexts, use different keys. And I'd recommend, not empty Strings.



    You can also use one key and store an object containing all your edittexts values structured the way you want, the way you should be able to retrieve data easily, for example using JSONArray and stringify it.



    The content of EditText2 will replace the content of EditText1 in your preferences since you are using the same key.



    And when you try to retrieve your content, the associated value of "" will be shown. LAST_TEXT1 and LAST_TEXT2 are just variables here. Keys are what is contained within the variable.



    I hope this will help.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 16:31









    gratien asimbahwe

    9702822




    9702822








    • 1




      You're right, the key is the error here. I will take a look at JSONArray then, thanks for the hint! For now editing the keys will do, so thanks.
      – timoruether
      Nov 11 at 17:43










    • Great! Do not hesitate to ask more informations or post other questions in case you have an issue
      – gratien asimbahwe
      Nov 11 at 17:46














    • 1




      You're right, the key is the error here. I will take a look at JSONArray then, thanks for the hint! For now editing the keys will do, so thanks.
      – timoruether
      Nov 11 at 17:43










    • Great! Do not hesitate to ask more informations or post other questions in case you have an issue
      – gratien asimbahwe
      Nov 11 at 17:46








    1




    1




    You're right, the key is the error here. I will take a look at JSONArray then, thanks for the hint! For now editing the keys will do, so thanks.
    – timoruether
    Nov 11 at 17:43




    You're right, the key is the error here. I will take a look at JSONArray then, thanks for the hint! For now editing the keys will do, so thanks.
    – timoruether
    Nov 11 at 17:43












    Great! Do not hesitate to ask more informations or post other questions in case you have an issue
    – gratien asimbahwe
    Nov 11 at 17:46




    Great! Do not hesitate to ask more informations or post other questions in case you have an issue
    – gratien asimbahwe
    Nov 11 at 17:46












    up vote
    0
    down vote













    1.SharedPreference saves data in key-value pair. You save a data for a key and when needed, use that same key to recover the value, even if the app is closed.



    In your case, you are using two variables for storing string-keys, named, LAST_TEXT1 and LAST_TEXT2. But sadly, both are carrying the same value, "" or empty string. As both keys are technically the same, you are getting the same result. Define Those two keys like this to get different results:



    public static final String LAST_TEXT1 = "LAST_TEXT1";
    public static final String LAST_TEXT2 = "LAST_TEXT2";


    2.SharedPreference is used for simple and short key-values. If your values are longer than few words, I suggest using Sqlite Database or similar long time data persisting methods.






    share|improve this answer

























      up vote
      0
      down vote













      1.SharedPreference saves data in key-value pair. You save a data for a key and when needed, use that same key to recover the value, even if the app is closed.



      In your case, you are using two variables for storing string-keys, named, LAST_TEXT1 and LAST_TEXT2. But sadly, both are carrying the same value, "" or empty string. As both keys are technically the same, you are getting the same result. Define Those two keys like this to get different results:



      public static final String LAST_TEXT1 = "LAST_TEXT1";
      public static final String LAST_TEXT2 = "LAST_TEXT2";


      2.SharedPreference is used for simple and short key-values. If your values are longer than few words, I suggest using Sqlite Database or similar long time data persisting methods.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        1.SharedPreference saves data in key-value pair. You save a data for a key and when needed, use that same key to recover the value, even if the app is closed.



        In your case, you are using two variables for storing string-keys, named, LAST_TEXT1 and LAST_TEXT2. But sadly, both are carrying the same value, "" or empty string. As both keys are technically the same, you are getting the same result. Define Those two keys like this to get different results:



        public static final String LAST_TEXT1 = "LAST_TEXT1";
        public static final String LAST_TEXT2 = "LAST_TEXT2";


        2.SharedPreference is used for simple and short key-values. If your values are longer than few words, I suggest using Sqlite Database or similar long time data persisting methods.






        share|improve this answer












        1.SharedPreference saves data in key-value pair. You save a data for a key and when needed, use that same key to recover the value, even if the app is closed.



        In your case, you are using two variables for storing string-keys, named, LAST_TEXT1 and LAST_TEXT2. But sadly, both are carrying the same value, "" or empty string. As both keys are technically the same, you are getting the same result. Define Those two keys like this to get different results:



        public static final String LAST_TEXT1 = "LAST_TEXT1";
        public static final String LAST_TEXT2 = "LAST_TEXT2";


        2.SharedPreference is used for simple and short key-values. If your values are longer than few words, I suggest using Sqlite Database or similar long time data persisting methods.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 16:39









        Touhidul Islam

        534114




        534114






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250666%2fsaving-several-edittexts-in-one-activity-efficiently-with-sharedpreferences%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ