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.
android android-studio android-edittext sharedpreferences
add a comment |
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.
android android-studio android-edittext sharedpreferences
are the edittexts inside and recyclerView or gridView?
– Kaveri
Nov 11 at 16:38
add a comment |
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.
android android-studio android-edittext sharedpreferences
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
android android-studio android-edittext sharedpreferences
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 16:39
Touhidul Islam
534114
534114
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
are the edittexts inside and recyclerView or gridView?
– Kaveri
Nov 11 at 16:38