-3

i have two edittext i want to save data in edittext until i have not change it how to made it . i try some thing like this but data not save.

e1 = (EditText)findViewById(R.id.editText);
    e2 = (EditText)findViewById(R.id.editText2);
    b1 = (Button)findViewById(R.id.button3);

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String homewifi  = e1.getText().toString();
            String officewifi  = e2.getText().toString();

            SharedPreferences.Editor editor = sharedpreferences.edit();

            editor.putString(HOMEWIFI, homewifi);
            editor.putString(OFFICEWIFI, officewifi);
            editor.commit();
            Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();

        }
    });

when activity open again data not show in edittext.

faisal iqbal
  • 694
  • 1
  • 6
  • 18

3 Answers3

4

You are not retrieving data from sharedpreferences modify code like this:

 sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);  
     SharedPreferences.Editor editor = sharedpreferences.edit();

    e1 = (EditText)findViewById(R.id.editText);
    e2 = (EditText)findViewById(R.id.editText2);

    String homewifi = sharedpreferences.getString(HOMEWIFI, "");
    String officeWifi = sharedpreferences.getString(OFFICEWIFI, "");

    e1.setText(homewifi);
    e2.setText(officeWifi);


        b1 = (Button)findViewById(R.id.button3);

       Context.MODE_PRIVATE);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String homewifi  = e1.getText().toString();
                String officewifi  = e2.getText().toString();



                editor.putString(HOMEWIFI, homewifi);
                editor.putString(OFFICEWIFI, officewifi);
                editor.commit();
                Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();

            }
        });
Suhas Bachewar
  • 1,234
  • 7
  • 20
1

when activity open again data not show in EditText.

Need to get data from SharedPreferences and call setText method of EditText's to show it when starting Activity Again.like:

....
e2 = (EditText)findViewById(R.id.editText2);
b1 = (Button)findViewById(R.id.button3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(HOMEWIFI)) {
   String strHomeWifi = settings.getString(HOMEWIFI, "default value");
   e1.setText(strHomeWifi); 
}else if (sharedpreferences.contains(OFFICEWIFI)) {
   String strofficewifi = settings.getString(OFFICEWIFI, "default value");
   e2.setText(strofficewifi); 
}
...
ρяσѕρєя K
  • 127,886
  • 50
  • 184
  • 206
0

I guess you forgot to set string from your SharedPreferences to both the edittext,

after saving it from edit text to sharedpreferences.

like

 e1 = (EditText)findViewById(R.id.editText);
            e2 = (EditText)findViewById(R.id.editText2);
            b1 = (Button)findViewById(R.id.button3);

            sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

            if(!TextUtils.isEmpty(sharedpreferences.getString(HOMEWIFI))){
                e1.setText(sharedpreferences.getString(HOMEWIFI));
            }
            if(!TextUtils.isEmpty(sharedpreferences.getString(OFFICEWIFI))){
                e1.setText(sharedpreferences.getString(OFFICEWIFI));
            }

            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String homewifi  = e1.getText().toString();
                    String officewifi  = e2.getText().toString();

                    SharedPreferences.Editor editor = sharedpreferences.edit();

                    editor.putString(HOMEWIFI, homewifi);
                    editor.putString(OFFICEWIFI, officewifi);
                    editor.commit();
                    Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_SHORT).show();

                }
            });
Electro
  • 355
  • 3
  • 15