2

Can you please tell me how can i use shared prefrences to write multiple entries in a shared prefrences file.Like if i want to add multiple names in the shared pref file i am using the following code but each time i click on submit button it overrides the previous entry.

public void onClick(View v) 
{           
    SharedPreferences settings = getSharedPreferences("users", 0);
    SharedPreferences.Editor editorUser = settings.edit();
    editorUser.putString("user", editUser.getText().toString());
    editorUser.commit();
}
Max
  • 12,622
  • 16
  • 73
  • 101
birring
  • 71
  • 1
  • 1
  • 7

3 Answers3

6

You have to use different keys, e.g.:

            SharedPreferences settings = getSharedPreferences("users", 0);

            SharedPreferences.Editor editorUser = settings.edit();
            for (int i = 0; i < users.size(); i++) 
                 editorUser.putString("user" + i, users.get(i));

            editorUser.commit();
Szymon
  • 42,577
  • 16
  • 96
  • 114
  • what if i have n numbers of users – birring Sep 16 '13 at 09:46
  • this might produce trouble as the sharedPref saves it persistent and there is no check if user exists already. – bofredo Sep 16 '13 at 10:13
  • Szymom answer is correct, if you use shared preference for saving datas. Try to use DB, this is an another way to store datas and to check if the user exists already or not – Kalai.G Sep 16 '13 at 10:20
  • Shared preferences are meant to store simple stuff without much complications. Also, the question wasn't about not overwriting the same key (at least that's how I understood it). – Szymon Sep 16 '13 at 10:22
  • I am a newbie in android.could you let me know how to use different keys to store values.anyone plz. – birring Sep 16 '13 at 10:32
0

This is how you can do this

Go through with these 2 URL this will help you

vogella.

mobile.tutsplus.com

Save

 public void saveInformation(String username,String password) {
        SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
        SharedPreferences.Editor editor = shared.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.commit();
    }

Load

private void LoadPreferences(){
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    String username = sharedPreferences.getString("username", "");
    String password = sharedPreferences.getString("password", "");

   }

Edit

 private void editDate(){
    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
    editor.putString("username", mSaved.getText().toString());
    editor.putInt("selection-start", mSaved.getSelectionStart());
    editor.putInt("selection-end", mSaved.getSelectionEnd());
    editor.commit();
   }

To save multiple users data in will suggest you to create multiple files for each user and then save it in shared pref

Just go through with the diffrence between getDefaultSharedPreferences and getSharedPreferences from here Diffrence Link

Community
  • 1
  • 1
Developer
  • 6,292
  • 19
  • 55
  • 115
  • thx for reply Gaurav but i am not getting it,it seems like same code written in different way. – birring Sep 16 '13 at 09:45
  • it's a function just pass the parameter to this function .and add all like i have done for username and password. and call it from where u want to save the data – Developer Sep 16 '13 at 09:48
  • @user2783369 see i have updated my answer if u need any help let me know – Developer Sep 16 '13 at 09:53
  • i am trying to use this code but it is not working for me Gaurav:( – birring Sep 16 '13 at 10:03
  • what actually u need to save the details of multiple users in shared pref i am updating my answer sure it will help you – Developer Sep 16 '13 at 10:04
  • how can this save different users and their passwords? you need some kind of list to achieve it like this here. – bofredo Sep 16 '13 at 10:11
0

You are facing that overriding issue because you are using the same shared preference key each time to save the value. Assuming that you are using the same EditText for multiple entries, I guess the following snippet will do the trick.

counter=0;
        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                  settings = getSharedPreferences("users", 0);
                    SharedPreferences.Editor editorUser = settings.edit();
                    editorUser.putString("user"+counter, Edittext.getText().toString());
                    editorUser.commit();
                    counter++;
                    Edittext.setText("");
            }
        });
        show.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                for(int i=0;i<counter;i++)
                {
                    Log.i("User "+i, ""+settings.getString("user"+i, ""));
                }
            }
        });

Here, I used the show button OnClickListner just to check for the multiple entries through EditText. But here I would like to suggest you to go for SQLite DB if you really wish to store multiple user info.

Spring Breaker
  • 8,233
  • 3
  • 36
  • 60