我在Android编程方面很陌生,我想原谅我做的任何错误,因为我是德国人:)
我正在为我的学校编写应用程序。
在这个应用程序中,我试图制作一个可编辑的时间表,以便每个人都可以写出他的个人时间表。我通过创建SharedPreferences
文件来做到这一点,但是当我编辑值并查看它们是否已更改时,值只是"默认值"而不是我在EditText
元素中输入的值。
这是我的代码:
@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;
}
}
这是应该是的活动,但只有"默认值" ...
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;
}
}
我希望你能理解我的问题并帮助我,我真的不知道该怎么做,而且我试图解决这个问题3个小时。
答案 0 :(得分:2)
只需将以下行添加到onClick
方法中:
editor.commit();
或者,如果您只支持API级别11及以上
editor.apply();
只是为了清除应用程序:您之前未保存更改。您必须致电apply()
或commit()
明确表示保存更改
从Docs开始:
将您的偏好设置更改从此编辑器返回到 它正在编辑的SharedPreferences对象。这原子地执行 要求修改,替换当前的任何内容 SharedPreferences。