Store email that are used to log in and show as suggestion on the email input during next Login - Android...












0















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.










share|improve this question





























    0















    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.










    share|improve this question



























      0












      0








      0








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 8:01







      Mill3r

















      asked Nov 19 '18 at 18:12









      Mill3rMill3r

      131315




      131315
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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) {
          }
          });
          }





          share|improve this answer

































            1














            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 enter image description here






            share|improve this answer


























            • 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 '18 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 '18 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 '18 at 8:08








            • 1





              try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb

              – ahuja007
              Nov 21 '18 at 8:15








            • 1





              I have added a new answer, as old answer shows about saving list in shared preferences

              – ahuja007
              Nov 21 '18 at 8:30











            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',
            autoActivateHeartbeat: false,
            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%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

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            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) {
            }
            });
            }





            share|improve this answer






























              2














              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) {
              }
              });
              }





              share|improve this answer




























                2












                2








                2







                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) {
                }
                });
                }





                share|improve this answer















                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) {
                }
                });
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 12:06









                bummi

                25.1k85289




                25.1k85289










                answered Nov 21 '18 at 8:30









                ahuja007ahuja007

                21119




                21119

























                    1














                    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 enter image description here






                    share|improve this answer


























                    • 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 '18 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 '18 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 '18 at 8:08








                    • 1





                      try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb

                      – ahuja007
                      Nov 21 '18 at 8:15








                    • 1





                      I have added a new answer, as old answer shows about saving list in shared preferences

                      – ahuja007
                      Nov 21 '18 at 8:30
















                    1














                    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 enter image description here






                    share|improve this answer


























                    • 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 '18 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 '18 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 '18 at 8:08








                    • 1





                      try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb

                      – ahuja007
                      Nov 21 '18 at 8:15








                    • 1





                      I have added a new answer, as old answer shows about saving list in shared preferences

                      – ahuja007
                      Nov 21 '18 at 8:30














                    1












                    1








                    1







                    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 enter image description here






                    share|improve this answer















                    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 enter image description here







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 21 '18 at 6:43

























                    answered Nov 20 '18 at 7:33









                    ahuja007ahuja007

                    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 '18 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 '18 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 '18 at 8:08








                    • 1





                      try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb

                      – ahuja007
                      Nov 21 '18 at 8:15








                    • 1





                      I have added a new answer, as old answer shows about saving list in shared preferences

                      – ahuja007
                      Nov 21 '18 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 '18 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 '18 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 '18 at 8:08








                    • 1





                      try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb

                      – ahuja007
                      Nov 21 '18 at 8:15








                    • 1





                      I have added a new answer, as old answer shows about saving list in shared preferences

                      – ahuja007
                      Nov 21 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 8:08






                    1




                    1





                    try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb

                    – ahuja007
                    Nov 21 '18 at 8:15







                    try this. I have edited your code gist.github.com/adrielAd/b1fbc4f8665183eb195871d21cd415eb

                    – ahuja007
                    Nov 21 '18 at 8:15






                    1




                    1





                    I have added a new answer, as old answer shows about saving list in shared preferences

                    – ahuja007
                    Nov 21 '18 at 8:30





                    I have added a new answer, as old answer shows about saving list in shared preferences

                    – ahuja007
                    Nov 21 '18 at 8:30


















                    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.




                    draft saved


                    draft discarded














                    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





















































                    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

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)