Load strings from sharedpreferences in EditText (two classes)

92 views Asked by At

I have two different activity's. First one only showed to the user first time - to store phonenumber inside sharedprefs. I think the problem is my load from prefs method but just in case I will leave everything here to make it more relevant to future users. It looks like in both activitys the "mobilnummer" is saved - but I am having problem to display them.

The idea is it to be overwritten if user updates it - I think the method will do that?

OnStartUp.java

public static final String PREFS_NAME = "MobileNumberSaved";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mobilnummer = (EditText) findViewById(R.id.mobilnummer);


    final Button button = (Button) findViewById(R.id.ntonboarding);}
        public void SaveToMobile(View v) {
            if (mobilnummer.getText().toString().length() >= 8) {
                FragmentedUser.SaveMobile(mobilnummer.getText().toString()); // just saves to Parse.com (This works)

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("mobilnummer", mobilnummer.toString());
                editor.commit();

        }
     }

Profile

 EditText mobilnummer;
public static final String PREFS_NAME = "MobileNumberSaved";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    mobilnummer = (EditText) findViewById(R.id.mobilnummer);

This is the issue/problem i think! (Code below)

        SharedPreferences MobileNumberSaved =   this.getSharedPreferences("mobilnummer", Context.MODE_PRIVATE);
        String tempMobilnummer = mobilnummer.getText().toString();
        mobilnummer.setText(tempMobilnummer);



       // IF user wants to change mobilenumber
       mobilnummer.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                if (mobilnummer.getText().toString().length() >= 8)
                    FragmentedUser.SaveMobile(mobilnummer.getText().toString());
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("mobilnummer", mobilnummer.toString());
                editor.commit();
                tlfstatus.setImageResource(R.drawable.ok);
                handled = true;
            }
            return handled;
        }
    });
1

There are 1 answers

1
Gabriella Angelova On BEST ANSWER

I think here is your error:

SharedPreferences MobileNumberSaved =   this.getSharedPreferences("mobilnummer", Context.MODE_PRIVATE);
//here it should be MobileNumberSaved instead of mobilnummer, because tehre is no sense to set the same text to the textView again after you got it from there
String tempMobilnummer = MobileNumberSaved.getText().toString();
mobilnummer.setText(tempMobilnummer);

But the correct way to get a value from shared preferences is something like this:

SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
String savedMobileNumber = prefs.getString("mobilnummer", "");
mobilnummer.setText(savedMobileNumber);

Update: when you save the value in the shared preferences, you save only the editText, not it's value, so do something like this when saving:

SharedPreferences.Editor editor = settings.edit();
//mobilnumber is your EditText, so get it's text like this
editor.putString("mobilnummer", mobilnummer.getText().toString());
editor.commit();

And everything else is still the same that I wrote above