1

File name = MainActivity.java

public class MainActivity extends Activity {

TextView textView1;
EditText editText1,editText2;
Button button1,button2;
public static final String Default = "NA";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView1 = (TextView)findViewById(R.id.textViewSignup);        
    textView1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
        Intent i = new Intent(MainActivity.this,Sign_up.class);
        startActivity(i);
        }
    });
    editText1=(EditText)findViewById(R.id.editText1);
    editText2=(EditText)findViewById(R.id.editText2);
    button1 =(Button)findViewById(R.id.button1);
    button2 =(Button)findViewById(R.id.button2);


    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            SharedPreferences preferences = getSharedPreferences("Login Credentials",MODE_PRIVATE);

            String firstname=preferences.getString("Name",Default );
            String email= preferences.getString("Email",Default );
            String password=preferences.getString("Password", Default);




            if(editText1.getText().toString().equals(firstname)&&
                    editText2.getText().toString().equals(password)){

                Toast.makeText(getApplicationContext(), "Welcome User", Toast.LENGTH_SHORT).show();
        }else
        {
            Toast.makeText(getApplicationContext(), "Wrong User", Toast.LENGTH_SHORT).show();

        }
            }
    });
    button2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
        finish();   
        }
    });



}

Second file name = Signup.java

public class Sign_up extends MainActivity implements OnClickListener{

   public static final String Default = "MyPrefs" ;


  String name,email,password,confirmpassword;
  EditText et1,et2,et3,et4;
  TextView textView;
  Button button;
  SharedPreferences preferences;
  Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign_up);

    et1 = (EditText)findViewById(R.id.editTextName);
    et2 = (EditText)findViewById(R.id.editEmail);
    et3 = (EditText)findViewById(R.id.editPassword);
    et4 = (EditText)findViewById(R.id.editConPassword);
    button = (Button)findViewById(R.id.buttonSignup);
    textView = (TextView)findViewById(R.id.textViewLoginhere);
    button.setOnClickListener(this);


    textView.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Sign_up.this,MainActivity.class);
            startActivity(intent);
        }
    });


}
//public void sharedPrefarence(){


//}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    preferences = getSharedPreferences("Login Credentials", MODE_PRIVATE);
    SharedPreferences.Editor editor=preferences.edit();
    //editor = preferences.edit();
    editor.putString("Name", et1.getText().toString());
    editor.putString("Email",et2.getText().toString());
    editor.putString("Password", et3.getText().toString());
    editor.putString("Confirmpassword", et4.getText().toString());
    editor.commit();

    Toast.makeText(getApplicationContext(), "Added Successful",
            Toast.LENGTH_SHORT).show();
}

}

This code is working but when i add new user then the key is overwrite thats why it is unable to store multiple values so please give me correct suggestion for this

Thanks & Regards Rakesh

Rakesh Kumar
  • 11
  • 1
  • 3
  • the preference system is a key-value dictionary, you assign a value to a key. A key can only have one value. – rupps Apr 14 '14 at 06:12

4 Answers4

1

Shared Preferences only stores simple name/value pairs. They key names are unique, so when you put values for "Name", "Email", "Password", and "Confirmpassword", it will always just overwrite what was there.

It looks like you need multiple sets of these fields, so you might want to store your data in a sqllite db.

hungryghost
  • 9,463
  • 3
  • 23
  • 36
0

You should try to use the database in order to store multiple records. The preference system is working as intended.

morenoadan22
  • 234
  • 1
  • 9
0

You need unique key in Shared Prefarence for each unique value. So onClick method either you make logic that always save your each user with unique key.

Or another way is that, you can save data in an ArrayList and then save that ArrayList in sharedPrefarence when you done with getting all user info saved in that ArrayList. To save ArrayList is SharedPrefarence check this link

And you can also use sqlite as well

Community
  • 1
  • 1
Mobi
  • 645
  • 3
  • 6
  • 14
0

Here's where i got it done:

[http://androidopentutorials.com/android-how-to-store-list-of-values-in-sharedpreferences/][1]

Much is done in subsection 1.2.2. Look at it keenly. You just need to Download and import the Gson Library provided by Google and the trick is done.

Akah
  • 1,389
  • 14
  • 19