Store email that are used to log in and show as suggestion on the email input during next Login - Android...
up vote
0
down vote
favorite
I am trying to find a way to keep all the email used for login into the application in a device and show them as suggestion or autocomplete when ever a user tries to login in to the application.
I tried using shared preferences and store them into an ArrayList during the login. However each time I login, the email gets replaced with the latest login email instead of adding up.
Example: first i login with admin@admin.com and logout from the system. For next login the suggestion admin@admin.com shows up. However if now i login with employee@employee.com, the old stored email is replaced with the new one.
What I did to get this was first after login I stored the user email from the server response in a shared preference.
Next I put that data into an ArrayList
SharedPreferences getEmail = getSharedPreferences("userEmail", Context.MODE_PRIVATE);
String input_email = getEmail.getString("emp_email", "");
ArrayList<String> userList = new ArrayList<>();
userList.add(input_email);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userList);
if (userList.size() > 0){
userEmail.setAdapter(adapter);
}
The above code in inside the onCreate method of the login activity. How can i do this.
I am guessing maybe this is not the correct way to remember email address for auto suggestion. If so then please point me to the right direction.
Thank you.
Update
Fetching response from the server in LoginActivity on successful login
public void getDataForId() {
SharedPreferences authToken = getSharedPreferences("authToken", Context.MODE_PRIVATE);
String token = authToken.getString("token", "");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<UserResponse> call = apiInterface.getData("Bearer " + token);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()) {
String emp_email = response.body().getUser().getEmail();
ArrayList<String> listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add(emp_email);
}
saveToPrefs(this, listEmails);
/*Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains(emp_email)){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());*/
} else {
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
}
});
}
Note that in am getting the email in the variable **emp_email* (assume the other two functions given by you are in the file). What I assume is that the commented out part (after the first email is saved), will start in the onCreate methode for me as after logging out.
there i get the pref data see it the new email matched the pref data and then store it. This is when it is getting replaced.
android arraylist login sharedpreferences
add a comment |
up vote
0
down vote
favorite
I am trying to find a way to keep all the email used for login into the application in a device and show them as suggestion or autocomplete when ever a user tries to login in to the application.
I tried using shared preferences and store them into an ArrayList during the login. However each time I login, the email gets replaced with the latest login email instead of adding up.
Example: first i login with admin@admin.com and logout from the system. For next login the suggestion admin@admin.com shows up. However if now i login with employee@employee.com, the old stored email is replaced with the new one.
What I did to get this was first after login I stored the user email from the server response in a shared preference.
Next I put that data into an ArrayList
SharedPreferences getEmail = getSharedPreferences("userEmail", Context.MODE_PRIVATE);
String input_email = getEmail.getString("emp_email", "");
ArrayList<String> userList = new ArrayList<>();
userList.add(input_email);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userList);
if (userList.size() > 0){
userEmail.setAdapter(adapter);
}
The above code in inside the onCreate method of the login activity. How can i do this.
I am guessing maybe this is not the correct way to remember email address for auto suggestion. If so then please point me to the right direction.
Thank you.
Update
Fetching response from the server in LoginActivity on successful login
public void getDataForId() {
SharedPreferences authToken = getSharedPreferences("authToken", Context.MODE_PRIVATE);
String token = authToken.getString("token", "");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<UserResponse> call = apiInterface.getData("Bearer " + token);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()) {
String emp_email = response.body().getUser().getEmail();
ArrayList<String> listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add(emp_email);
}
saveToPrefs(this, listEmails);
/*Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains(emp_email)){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());*/
} else {
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
}
});
}
Note that in am getting the email in the variable **emp_email* (assume the other two functions given by you are in the file). What I assume is that the commented out part (after the first email is saved), will start in the onCreate methode for me as after logging out.
there i get the pref data see it the new email matched the pref data and then store it. This is when it is getting replaced.
android arraylist login sharedpreferences
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to find a way to keep all the email used for login into the application in a device and show them as suggestion or autocomplete when ever a user tries to login in to the application.
I tried using shared preferences and store them into an ArrayList during the login. However each time I login, the email gets replaced with the latest login email instead of adding up.
Example: first i login with admin@admin.com and logout from the system. For next login the suggestion admin@admin.com shows up. However if now i login with employee@employee.com, the old stored email is replaced with the new one.
What I did to get this was first after login I stored the user email from the server response in a shared preference.
Next I put that data into an ArrayList
SharedPreferences getEmail = getSharedPreferences("userEmail", Context.MODE_PRIVATE);
String input_email = getEmail.getString("emp_email", "");
ArrayList<String> userList = new ArrayList<>();
userList.add(input_email);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userList);
if (userList.size() > 0){
userEmail.setAdapter(adapter);
}
The above code in inside the onCreate method of the login activity. How can i do this.
I am guessing maybe this is not the correct way to remember email address for auto suggestion. If so then please point me to the right direction.
Thank you.
Update
Fetching response from the server in LoginActivity on successful login
public void getDataForId() {
SharedPreferences authToken = getSharedPreferences("authToken", Context.MODE_PRIVATE);
String token = authToken.getString("token", "");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<UserResponse> call = apiInterface.getData("Bearer " + token);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()) {
String emp_email = response.body().getUser().getEmail();
ArrayList<String> listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add(emp_email);
}
saveToPrefs(this, listEmails);
/*Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains(emp_email)){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());*/
} else {
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
}
});
}
Note that in am getting the email in the variable **emp_email* (assume the other two functions given by you are in the file). What I assume is that the commented out part (after the first email is saved), will start in the onCreate methode for me as after logging out.
there i get the pref data see it the new email matched the pref data and then store it. This is when it is getting replaced.
android arraylist login sharedpreferences
I am trying to find a way to keep all the email used for login into the application in a device and show them as suggestion or autocomplete when ever a user tries to login in to the application.
I tried using shared preferences and store them into an ArrayList during the login. However each time I login, the email gets replaced with the latest login email instead of adding up.
Example: first i login with admin@admin.com and logout from the system. For next login the suggestion admin@admin.com shows up. However if now i login with employee@employee.com, the old stored email is replaced with the new one.
What I did to get this was first after login I stored the user email from the server response in a shared preference.
Next I put that data into an ArrayList
SharedPreferences getEmail = getSharedPreferences("userEmail", Context.MODE_PRIVATE);
String input_email = getEmail.getString("emp_email", "");
ArrayList<String> userList = new ArrayList<>();
userList.add(input_email);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userList);
if (userList.size() > 0){
userEmail.setAdapter(adapter);
}
The above code in inside the onCreate method of the login activity. How can i do this.
I am guessing maybe this is not the correct way to remember email address for auto suggestion. If so then please point me to the right direction.
Thank you.
Update
Fetching response from the server in LoginActivity on successful login
public void getDataForId() {
SharedPreferences authToken = getSharedPreferences("authToken", Context.MODE_PRIVATE);
String token = authToken.getString("token", "");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<UserResponse> call = apiInterface.getData("Bearer " + token);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()) {
String emp_email = response.body().getUser().getEmail();
ArrayList<String> listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add(emp_email);
}
saveToPrefs(this, listEmails);
/*Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains(emp_email)){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());*/
} else {
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
}
});
}
Note that in am getting the email in the variable **emp_email* (assume the other two functions given by you are in the file). What I assume is that the commented out part (after the first email is saved), will start in the onCreate methode for me as after logging out.
there i get the pref data see it the new email matched the pref data and then store it. This is when it is getting replaced.
android arraylist login sharedpreferences
android arraylist login sharedpreferences
edited Nov 21 at 8:01
asked Nov 19 at 18:12
Mill3r
129214
129214
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Try this.
I have edited your code
gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
// make listEmails global and also initialise this in onCreate so you get already saved emails when you come to this screen
ArrayList<String> listEmails;
public void getDataForId() {
SharedPreferences authToken = getSharedPreferences("authToken", Context.MODE_PRIVATE);
String token = authToken.getString("token", "");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<UserResponse> call = apiInterface.getData("Bearer " + token);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()) {
String emp_email = response.body().getUser().getEmail();
listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add(emp_email);
}else{
if(!listEmails.contains(emp_email)){
listEmails.add(emp_email);
}
}
saveToPrefs(this, listEmails);
/*Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains(emp_email)){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());*/
} else {
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
}
});
}
add a comment |
up vote
1
down vote
You can do like below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add("a@a.a");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains("b@b.b")){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
}
public static void saveToPrefs(Context context, ArrayList<String> listEmail) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String list = gson.toJson(listEmail);
prefsEditor.putString("list", list);
prefsEditor.commit();
}
public static ArrayList<String> getFromPrefs(Context context) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
Gson gson = new Gson();
String list = appSharedPrefs.getString("list", "");
ArrayList<String> listEmail = gson.fromJson(list, ArrayList.class);
return listEmail;
}
Here is the screenshot for logs
Thank you very much for your advise. I have been busy all day with work so i could not check it. I will do asap and give a feedback for others to see.
– Mill3r
Nov 20 at 14:22
what is did wasArrayList<String> userList = new ArrayList<>();
at the start of the class. Then I added the server response of the email to the userList likeString emp_email = response.body().getUser().getEmail(); if (!userList.contains(emp_email)){ userList.add(emp_email); }
then saved it to tinyBD likeTinyDB tinydb = new TinyDB(LoginActivity.this); tinydb.putListString("user_email_id", userList);
Now, when i get this string, i still only get the last logged in user id like before.
– Mill3r
Nov 20 at 18:10
1
try adding this in your code if(listEmails == null){ listEmails = new ArrayList<>(); listEmails.add(emp_email); }else{ if(!listEmails.contains(emp_email)){ listEmails.add(emp_email); } }
– ahuja007
Nov 21 at 8:08
1
try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
– ahuja007
Nov 21 at 8:15
1
I have added a new answer, as old answer shows about saving list in shared preferences
– ahuja007
Nov 21 at 8:30
|
show 9 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Try this.
I have edited your code
gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
// make listEmails global and also initialise this in onCreate so you get already saved emails when you come to this screen
ArrayList<String> listEmails;
public void getDataForId() {
SharedPreferences authToken = getSharedPreferences("authToken", Context.MODE_PRIVATE);
String token = authToken.getString("token", "");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<UserResponse> call = apiInterface.getData("Bearer " + token);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()) {
String emp_email = response.body().getUser().getEmail();
listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add(emp_email);
}else{
if(!listEmails.contains(emp_email)){
listEmails.add(emp_email);
}
}
saveToPrefs(this, listEmails);
/*Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains(emp_email)){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());*/
} else {
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
}
});
}
add a comment |
up vote
2
down vote
accepted
Try this.
I have edited your code
gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
// make listEmails global and also initialise this in onCreate so you get already saved emails when you come to this screen
ArrayList<String> listEmails;
public void getDataForId() {
SharedPreferences authToken = getSharedPreferences("authToken", Context.MODE_PRIVATE);
String token = authToken.getString("token", "");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<UserResponse> call = apiInterface.getData("Bearer " + token);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()) {
String emp_email = response.body().getUser().getEmail();
listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add(emp_email);
}else{
if(!listEmails.contains(emp_email)){
listEmails.add(emp_email);
}
}
saveToPrefs(this, listEmails);
/*Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains(emp_email)){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());*/
} else {
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
}
});
}
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Try this.
I have edited your code
gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
// make listEmails global and also initialise this in onCreate so you get already saved emails when you come to this screen
ArrayList<String> listEmails;
public void getDataForId() {
SharedPreferences authToken = getSharedPreferences("authToken", Context.MODE_PRIVATE);
String token = authToken.getString("token", "");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<UserResponse> call = apiInterface.getData("Bearer " + token);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()) {
String emp_email = response.body().getUser().getEmail();
listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add(emp_email);
}else{
if(!listEmails.contains(emp_email)){
listEmails.add(emp_email);
}
}
saveToPrefs(this, listEmails);
/*Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains(emp_email)){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());*/
} else {
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
}
});
}
Try this.
I have edited your code
gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
// make listEmails global and also initialise this in onCreate so you get already saved emails when you come to this screen
ArrayList<String> listEmails;
public void getDataForId() {
SharedPreferences authToken = getSharedPreferences("authToken", Context.MODE_PRIVATE);
String token = authToken.getString("token", "");
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<UserResponse> call = apiInterface.getData("Bearer " + token);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()) {
String emp_email = response.body().getUser().getEmail();
listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add(emp_email);
}else{
if(!listEmails.contains(emp_email)){
listEmails.add(emp_email);
}
}
saveToPrefs(this, listEmails);
/*Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains(emp_email)){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());*/
} else {
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
}
});
}
edited Nov 21 at 12:06
bummi
25k85288
25k85288
answered Nov 21 at 8:30
ahuja007
21119
21119
add a comment |
add a comment |
up vote
1
down vote
You can do like below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add("a@a.a");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains("b@b.b")){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
}
public static void saveToPrefs(Context context, ArrayList<String> listEmail) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String list = gson.toJson(listEmail);
prefsEditor.putString("list", list);
prefsEditor.commit();
}
public static ArrayList<String> getFromPrefs(Context context) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
Gson gson = new Gson();
String list = appSharedPrefs.getString("list", "");
ArrayList<String> listEmail = gson.fromJson(list, ArrayList.class);
return listEmail;
}
Here is the screenshot for logs
Thank you very much for your advise. I have been busy all day with work so i could not check it. I will do asap and give a feedback for others to see.
– Mill3r
Nov 20 at 14:22
what is did wasArrayList<String> userList = new ArrayList<>();
at the start of the class. Then I added the server response of the email to the userList likeString emp_email = response.body().getUser().getEmail(); if (!userList.contains(emp_email)){ userList.add(emp_email); }
then saved it to tinyBD likeTinyDB tinydb = new TinyDB(LoginActivity.this); tinydb.putListString("user_email_id", userList);
Now, when i get this string, i still only get the last logged in user id like before.
– Mill3r
Nov 20 at 18:10
1
try adding this in your code if(listEmails == null){ listEmails = new ArrayList<>(); listEmails.add(emp_email); }else{ if(!listEmails.contains(emp_email)){ listEmails.add(emp_email); } }
– ahuja007
Nov 21 at 8:08
1
try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
– ahuja007
Nov 21 at 8:15
1
I have added a new answer, as old answer shows about saving list in shared preferences
– ahuja007
Nov 21 at 8:30
|
show 9 more comments
up vote
1
down vote
You can do like below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add("a@a.a");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains("b@b.b")){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
}
public static void saveToPrefs(Context context, ArrayList<String> listEmail) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String list = gson.toJson(listEmail);
prefsEditor.putString("list", list);
prefsEditor.commit();
}
public static ArrayList<String> getFromPrefs(Context context) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
Gson gson = new Gson();
String list = appSharedPrefs.getString("list", "");
ArrayList<String> listEmail = gson.fromJson(list, ArrayList.class);
return listEmail;
}
Here is the screenshot for logs
Thank you very much for your advise. I have been busy all day with work so i could not check it. I will do asap and give a feedback for others to see.
– Mill3r
Nov 20 at 14:22
what is did wasArrayList<String> userList = new ArrayList<>();
at the start of the class. Then I added the server response of the email to the userList likeString emp_email = response.body().getUser().getEmail(); if (!userList.contains(emp_email)){ userList.add(emp_email); }
then saved it to tinyBD likeTinyDB tinydb = new TinyDB(LoginActivity.this); tinydb.putListString("user_email_id", userList);
Now, when i get this string, i still only get the last logged in user id like before.
– Mill3r
Nov 20 at 18:10
1
try adding this in your code if(listEmails == null){ listEmails = new ArrayList<>(); listEmails.add(emp_email); }else{ if(!listEmails.contains(emp_email)){ listEmails.add(emp_email); } }
– ahuja007
Nov 21 at 8:08
1
try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
– ahuja007
Nov 21 at 8:15
1
I have added a new answer, as old answer shows about saving list in shared preferences
– ahuja007
Nov 21 at 8:30
|
show 9 more comments
up vote
1
down vote
up vote
1
down vote
You can do like below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add("a@a.a");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains("b@b.b")){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
}
public static void saveToPrefs(Context context, ArrayList<String> listEmail) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String list = gson.toJson(listEmail);
prefsEditor.putString("list", list);
prefsEditor.commit();
}
public static ArrayList<String> getFromPrefs(Context context) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
Gson gson = new Gson();
String list = appSharedPrefs.getString("list", "");
ArrayList<String> listEmail = gson.fromJson(list, ArrayList.class);
return listEmail;
}
Here is the screenshot for logs
You can do like below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> listEmails = getFromPrefs(this);
if(listEmails == null){
listEmails = new ArrayList<>();
listEmails.add("a@a.a");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
if(!listEmails.contains("b@b.b")){
listEmails.add("b@b.b");
}
saveToPrefs(this, listEmails);
Log.i("MainActivity", "onCreate: " + getFromPrefs(this).toString());
}
public static void saveToPrefs(Context context, ArrayList<String> listEmail) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String list = gson.toJson(listEmail);
prefsEditor.putString("list", list);
prefsEditor.commit();
}
public static ArrayList<String> getFromPrefs(Context context) {
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());
Gson gson = new Gson();
String list = appSharedPrefs.getString("list", "");
ArrayList<String> listEmail = gson.fromJson(list, ArrayList.class);
return listEmail;
}
Here is the screenshot for logs
edited Nov 21 at 6:43
answered Nov 20 at 7:33
ahuja007
21119
21119
Thank you very much for your advise. I have been busy all day with work so i could not check it. I will do asap and give a feedback for others to see.
– Mill3r
Nov 20 at 14:22
what is did wasArrayList<String> userList = new ArrayList<>();
at the start of the class. Then I added the server response of the email to the userList likeString emp_email = response.body().getUser().getEmail(); if (!userList.contains(emp_email)){ userList.add(emp_email); }
then saved it to tinyBD likeTinyDB tinydb = new TinyDB(LoginActivity.this); tinydb.putListString("user_email_id", userList);
Now, when i get this string, i still only get the last logged in user id like before.
– Mill3r
Nov 20 at 18:10
1
try adding this in your code if(listEmails == null){ listEmails = new ArrayList<>(); listEmails.add(emp_email); }else{ if(!listEmails.contains(emp_email)){ listEmails.add(emp_email); } }
– ahuja007
Nov 21 at 8:08
1
try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
– ahuja007
Nov 21 at 8:15
1
I have added a new answer, as old answer shows about saving list in shared preferences
– ahuja007
Nov 21 at 8:30
|
show 9 more comments
Thank you very much for your advise. I have been busy all day with work so i could not check it. I will do asap and give a feedback for others to see.
– Mill3r
Nov 20 at 14:22
what is did wasArrayList<String> userList = new ArrayList<>();
at the start of the class. Then I added the server response of the email to the userList likeString emp_email = response.body().getUser().getEmail(); if (!userList.contains(emp_email)){ userList.add(emp_email); }
then saved it to tinyBD likeTinyDB tinydb = new TinyDB(LoginActivity.this); tinydb.putListString("user_email_id", userList);
Now, when i get this string, i still only get the last logged in user id like before.
– Mill3r
Nov 20 at 18:10
1
try adding this in your code if(listEmails == null){ listEmails = new ArrayList<>(); listEmails.add(emp_email); }else{ if(!listEmails.contains(emp_email)){ listEmails.add(emp_email); } }
– ahuja007
Nov 21 at 8:08
1
try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
– ahuja007
Nov 21 at 8:15
1
I have added a new answer, as old answer shows about saving list in shared preferences
– ahuja007
Nov 21 at 8:30
Thank you very much for your advise. I have been busy all day with work so i could not check it. I will do asap and give a feedback for others to see.
– Mill3r
Nov 20 at 14:22
Thank you very much for your advise. I have been busy all day with work so i could not check it. I will do asap and give a feedback for others to see.
– Mill3r
Nov 20 at 14:22
what is did was
ArrayList<String> userList = new ArrayList<>();
at the start of the class. Then I added the server response of the email to the userList like String emp_email = response.body().getUser().getEmail(); if (!userList.contains(emp_email)){ userList.add(emp_email); }
then saved it to tinyBD like TinyDB tinydb = new TinyDB(LoginActivity.this); tinydb.putListString("user_email_id", userList);
Now, when i get this string, i still only get the last logged in user id like before.– Mill3r
Nov 20 at 18:10
what is did was
ArrayList<String> userList = new ArrayList<>();
at the start of the class. Then I added the server response of the email to the userList like String emp_email = response.body().getUser().getEmail(); if (!userList.contains(emp_email)){ userList.add(emp_email); }
then saved it to tinyBD like TinyDB tinydb = new TinyDB(LoginActivity.this); tinydb.putListString("user_email_id", userList);
Now, when i get this string, i still only get the last logged in user id like before.– Mill3r
Nov 20 at 18:10
1
1
try adding this in your code if(listEmails == null){ listEmails = new ArrayList<>(); listEmails.add(emp_email); }else{ if(!listEmails.contains(emp_email)){ listEmails.add(emp_email); } }
– ahuja007
Nov 21 at 8:08
try adding this in your code if(listEmails == null){ listEmails = new ArrayList<>(); listEmails.add(emp_email); }else{ if(!listEmails.contains(emp_email)){ listEmails.add(emp_email); } }
– ahuja007
Nov 21 at 8:08
1
1
try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
– ahuja007
Nov 21 at 8:15
try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb
– ahuja007
Nov 21 at 8:15
1
1
I have added a new answer, as old answer shows about saving list in shared preferences
– ahuja007
Nov 21 at 8:30
I have added a new answer, as old answer shows about saving list in shared preferences
– ahuja007
Nov 21 at 8:30
|
show 9 more comments
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%2f53380416%2fstore-email-that-are-used-to-log-in-and-show-as-suggestion-on-the-email-input-du%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