Etsipse android preferensfile; can't read key values

I am new to Android programming and I want to excuse myself for any mistakes I have made because I am German :)

I am programming an application for my school. In this application, I tried to make an editable schedule so that everyone can write their own personal schedule. I did this by creating a file SharedPreferences

, but when I change the values ​​and see if they have changed, the values ​​are just the "defaults" and not the values ​​entered in the element EditText

.

Here is my code:

Change action

@SuppressLint("CommitPrefEdits")
public class MondayEditActivity extends Activity {
public static final String PREFS_NAME = "MyPreferencesFile";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mondayedit);

    final EditText stunde1 = (EditText)findViewById(R.id.getStunde1); 
    //Stunde is german for hour
    final EditText stunde2 = (EditText)findViewById(R.id.getStunde2);
    // get Stunde = Edit Text element
    final EditText stunde3 = (EditText)findViewById(R.id.getStunde3);
    final EditText stunde4 = (EditText)findViewById(R.id.getStunde4);
    final EditText stunde5 = (EditText)findViewById(R.id.getStunde5);
    final EditText stunde6 = (EditText)findViewById(R.id.getStunde6);
    final EditText stunde7 = (EditText)findViewById(R.id.getStunde7);
    final EditText stunde8 = (EditText)findViewById(R.id.getStunde8);
    final EditText stunde9 = (EditText)findViewById(R.id.getStunde9);
    final EditText stunde10 = (EditText)findViewById(R.id.getStunde10);
    Button btn = (Button)findViewById(R.id.bestaetigen);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("stunde1", stunde1.getText().toString());
            editor.putString("stunde2", stunde2.getText().toString());
            editor.putString("stunde3", stunde3.getText().toString());
            editor.putString("stunde4", stunde4.getText().toString());
            editor.putString("stunde5", stunde5.getText().toString());
            editor.putString("stunde6", stunde6.getText().toString());
            editor.putString("stunde7", stunde7.getText().toString());
            editor.putString("stunde8", stunde8.getText().toString());
            editor.putString("stunde9", stunde9.getText().toString());
            editor.putString("stunde10", stunde10.getText().toString());

            startActivity(new Intent(MondayEditActivity.this, MondayActivity.class));

        }
    });
   }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.monday_edit, menu);
        return true;
     }

}

      

This is the action it should be in, but there is only a "default" ...

public class MondayActivity extends Activity {

    public static final String PREFS_NAME = "MyPreferencesFile";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monday);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

        TextView tvStunde1 = (TextView)findViewById(R.id.textviewstunde1);
        TextView tvStunde2 = (TextView)findViewById(R.id.textviewstunde2);
        TextView tvStunde3 = (TextView)findViewById(R.id.textviewstunde3);
        TextView tvStunde4 = (TextView)findViewById(R.id.textviewstunde4);
        TextView tvStunde5 = (TextView)findViewById(R.id.textviewstunde5);
        TextView tvStunde6 = (TextView)findViewById(R.id.textviewstunde6);
        TextView tvStunde7 = (TextView)findViewById(R.id.textviewstunde7);
        TextView tvStunde8 = (TextView)findViewById(R.id.textviewstunde8);
        TextView tvStunde9 = (TextView)findViewById(R.id.textviewstunde9);
        TextView tvStunde10 = (TextView)findViewById(R.id.textviewstunde10);

        tvStunde1.setText(settings.getString("stunde1", "-"));
        tvStunde2.setText(settings.getString("stunde2", "-"));
        tvStunde3.setText(settings.getString("stunde3", "-"));
        tvStunde4.setText(settings.getString("stunde4", "-"));
        tvStunde5.setText(settings.getString("stunde5", "-"));
        tvStunde6.setText(settings.getString("stunde6", "-"));
        tvStunde7.setText(settings.getString("stunde7", "-"));
        tvStunde8.setText(settings.getString("stunde8", "-"));
        tvStunde9.setText(settings.getString("stunde9", "-"));
        tvStunde10.setText(settings.getString("stunde10", "-"));



    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.monday, menu);
        return true;
    }

}

      

Hope you can understand my problem and help me, I really don't know what to do and I am trying to solve the problem within 3 hours.

+3


source to share


1 answer


Just add the following line to your method onClick

:

editor.commit();

      

or alternatively if you only support API level 11 and above



editor.apply();

      

Just clean the app: your changes have not been saved before. To save your changes, you need to call apply()

or commit()

. Straight from the Docs :

Copy the settings changes from this editor to the SharedPreferences Object that it is editing. This atomically performs the requested changes, replacing everything currently in SharedPreferences.

+2


source







All Articles