仅在重新启动活动之后显示共享频率
Hello Together 我开始编写我的第一个 Android 应用程序,并尝试使用 SharedPreferences 来存储一些字符串。 我可以输入不同的名称,然后在 onStop() 中将它们放入 SharedPreferences 中,然后进行提交。有人有解决方案,可以立即在下一个活动中显示首选项吗?因为此刻我必须切换回输入名称的活动,并且如果我立即切换回应显示名称的活动,它们就会出现。
protected void onStop() {
SharedPreferences.Editor edit = set.edit();
for(int x=0;x<counterM;x++){
edit.putString("playerM"+x, playersMale.get(x));
}
for(int x=0;x<counterF;x++){
edit.putString("playerF"+x, playersFemale.get(x));
}
edit.putInt("counter", counterF + counterM);
edit.commit();
super.onStop();
}
这是我加载名称的 onCreate()...
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
set = this.getSharedPreferences("MY_COUNT",0);
this.setContentView(R.layout.decisionscreen);
Random r = new Random();
int i = set.getInt("counter",1000);
int x = r.nextInt(i);
name = set.getString("playerM"+x, "no Players found");
TextView t = (TextView)findViewById(R.id.nameView);
t.setText(name+" "+i);
dareButton();
truthButton();
}
我希望有人能找出我的问题是什么。
Hello Together I startet to write my first Android app and I tried to use the SharedPreferences to have the possibility to store some strings.
I can Type in different names and in the onStop() I put them into the SharedPreferences and after that i make a commit. Has someone a solution that Preferences are shown on the next Activity immediatly? Because at the moment I have to switch back to the Activity where I Typed in the names and if I immediatly switch back to the Activity where the names shall be shown they appear.
protected void onStop() {
SharedPreferences.Editor edit = set.edit();
for(int x=0;x<counterM;x++){
edit.putString("playerM"+x, playersMale.get(x));
}
for(int x=0;x<counterF;x++){
edit.putString("playerF"+x, playersFemale.get(x));
}
edit.putInt("counter", counterF + counterM);
edit.commit();
super.onStop();
}
and here is the onCreate() where I load the name...
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
set = this.getSharedPreferences("MY_COUNT",0);
this.setContentView(R.layout.decisionscreen);
Random r = new Random();
int i = set.getInt("counter",1000);
int x = r.nextInt(i);
name = set.getString("playerM"+x, "no Players found");
TextView t = (TextView)findViewById(R.id.nameView);
t.setText(name+" "+i);
dareButton();
truthButton();
}
I hope someone can figure out what my problem is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将共享首选项编辑器内容放在 onPause 覆盖中。一旦您切换活动,它就会被调用。
Try to put the shared preferences editor stuff in the onPause override. It will be called as soon as you switch the activity.