仅在重新启动活动之后显示共享频率

发布于 2024-12-04 18:50:32 字数 1207 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2024-12-11 18:50:32

尝试将共享首选项编辑器内容放在 onPause 覆盖中。一旦您切换活动,它就会被调用。

Try to put the shared preferences editor stuff in the onPause override. It will be called as soon as you switch the activity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文