共享首选项出现在屏幕上

时间:2017-07-29 04:02:29

标签: android sharedpreferences saving-data

我正在为夏令营创建一个出勤跟踪器,辅导员可以通过键入editText并按下保存按钮输入他们的露营者签到的时间。 此基本字符串应保存到文本框中,并在每次加载应用时加载到屏幕上。有这样的多个方框,所以辅导员可以跟踪每个学生每天进/出的时间。

当按下按钮时,我使用sharedPreferences来保存顾问的输入,然后使用另一个按钮显示它。但是,当我关闭并重新启动APP时,我无法在屏幕上显示文字。我的代码遗漏了什么吗?

public class AttendancePage extends AppCompatActivity {

EditText mondayMorn;
TextView displayMonMorn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_attendance_page);
    String counsellorName = getIntent().getStringExtra("Senior Counsellor Name");

    TextView tv = (TextView)findViewById(R.id.counsellorName);
    tv.setText(counsellorName + "'s");

    mondayMorn = (EditText) findViewById(R.id.editText37);
    displayMonMorn = (TextView) findViewById(R.id.displayMonMorn);
}

public void saveInput (View view) {
    SharedPreferences checkInMon = getSharedPreferences("LoginTime", Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = checkInMon.edit();
    editor.putString("mondayIn", mondayMorn.getText().toString());
    editor.apply();

    Toast.makeText(this, "Saved", Toast.LENGTH_LONG).show();
}

public void updateSettings (View view){
    SharedPreferences checkInMon = getSharedPreferences("LoginTime", Context.MODE_PRIVATE);

    String time = checkInMon.getString("mondayIn", "");

    displayMonMorn.setText(time);
}

1 个答案:

答案 0 :(得分:0)

替换:

SharedPreferences.Editor editor = checkInMon.edit();
editor.putString("mondayIn", mondayMorn.getText().toString());
editor.apply();

使用:

SharedPreferences.Editor editor = checkInMon.edit();
editor.putString("mondayIn", mondayMorn.getText().toString());
editor.commit();

至少应该保存首选项中的数据。

相关问题