繁体   English   中英

在Android警报对话框中保存单选按钮选择

[英]Save radio button selection in Android alert dialog

我可以在使用应用程序时保存选择,但是每当我关闭应用程序并重新启动它时,选择都将再次为空。 我要去哪里错了? 它始终加载默认的“ 0”,而不记住最后的选择。

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = preferences.edit();
        SharedPreferences choiceSettings = getSharedPreferences("currentChoice", 0);
        final int[] currentChoice = {choiceSettings.getInt("currentChoice", 0)};
        final CharSequence[] items = {"AT&T", "Tmobile", "Verizon", "Sprint", "Other"};
        // Decide which carrier, so we can apply the correct forwarding code.
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Select your carrier");
        builder.setIcon(R.drawable.ic_launcher);
        builder.setSingleChoiceItems(items, currentChoice[0],
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        // TODO Auto-generated method stub
                        switch (item) {
                            case 0:
                                Toast.makeText(getApplicationContext(),
                                        items[item], Toast.LENGTH_SHORT).show();
                                // Your code when first option seletced
                                currentChoice[0] = 0;
                                editor.putInt(String.valueOf(currentChoice[0]), 0);
                                editor.putString("fCode", "*67*");
                                editor.apply();
                                break;
                            case 1:
                                // Your code when 2nd option seletced
                                Toast.makeText(getApplicationContext(),
                                        items[item], Toast.LENGTH_SHORT).show();
                                currentChoice[0] = 1;
                                editor.putInt(String.valueOf(currentChoice[0]), 0);
                                editor.putString("fCode", "*67*");
                                editor.apply();
                                break;
                            case 2:
                                // Your code when 2nd option seletced
                                Toast.makeText(getApplicationContext(),
                                        items[item], Toast.LENGTH_SHORT).show();
                                currentChoice[0] = 2;
                                editor.putInt(String.valueOf(currentChoice[0]), 0);
                                editor.putString("fCode", "*67*");
                                editor.apply();
                                break;
                        }
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

您正在使用两个共享的首选项容器,默认的一个和自定义的一个。 仅使用默认值。

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
final SharedPreferences.Editor editor = preferences.edit();
final int[] currentChoice = {preferences.getInt("currentChoice", 0)};

在开始(第4行)中,您尝试加载一个名为"currentChoice"的变量:

final int[] currentChoice = {choiceSettings.getInt("currentChoice", 0)};

通常,您可以使用editor.putInt(String key, int value)保存一个变量。 因此, key是用于保存变量的名称。

当你写

currentChoice[0] = 0;
editor.putInt(String.valueOf(currentChoice[0]), 0);

String.valueOf(currentChoice[0])变为“ 0”。 您将int 0保存到名为“ 0”的变量。 因此将第二行更改为

editor.putInt("currentChoice", currentChoice[0]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM