简体   繁体   中英

Save radio button selection in Android alert dialog

I can save the choice while I am using the app, but whenever I close the app and restart it, the choices are empty again. Where am I going wrong? It is always loading the default "0" instead of remembering the last selection.

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();
    }

You are using two shared preferences container, the default one and a custom one. Use only the default one.

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

In the beginning (line 4), you try to load a variable named "currentChoice" :

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

In general you save a variable with editor.putInt(String key, int value) . So key is the name used for saving your variable.

When you write

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

String.valueOf(currentChoice[0]) becomes "0". You save an int 0 to a variable named "0". So change the second line to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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