将微调器选择保存到共享首选项

发布于 2025-01-07 09:45:15 字数 2162 浏览 2 评论 0原文

我正在尝试将微调器的选择保存到共享首选项,但无法保存它。我保存文本没有问题,但旋转器似乎有点困难。我正在使用我在网上找到的首选项连接器,它似乎适用于文本,因此这可能是问题的一部分。这是一小段代码,仅包含头发颜色选择器微调器。我认为当我尝试阅读 SP (readPerson()) 时,我的问题就出现了,但我不完全确定。如果您有任何建议,请告诉我。谢谢!

    public class PreferencesActivity extends Activity {
    Spinner hairSpinner; //hair color selector

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_profile);
        init();
    }
    private void init() {
          hairSpinner = (Spinner) findViewById(R.id.hair_spinner);
          ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                  this, R.array.hair_array, android.R.layout.simple_spinner_item);
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          hairSpinner.setAdapter(adapter);   
    }
    public void save(View view) {
        int hairText = hairSpinner.getSelectedItemPosition();

        if (hairText != 100)
            PreferenceConnector.writeInteger(this, PreferenceConnector.HAIR,
                    hairText);
    }
    private void readPerson() {
        hairSpinner.setSelection(getText(PreferenceConnector.readInteger(this,
                PreferenceConnector.HAIR, 0)));
    }
}

这是我在网上找到的部分(其中的一部分):

    public class PreferenceConnector{
    public static final String PREF_NAME = "PEOPLE_PREFERENCES";
    public static final int MODE = Context.MODE_PRIVATE;

    public static final String HAIR = "HAIR";

    public static void writeInteger(Context context, String key, int value) {
        getEditor(context).putInt(key, value).commit();

    }

    public static int readInteger(Context context, String key, int defValue) {
        return getPreferences(context).getInt(key, defValue);
    }

    public static void writeString(Context context, String key, String value) {
        getEditor(context).putString(key, value).commit();

    }

    public static String readString(Context context, String key, String defValue) {
        return getPreferences(context).getString(key, defValue);
    }
}

I'm trying to save a spinner's selection to a shared preference, but I'm having trouble getting it to save. I have no problem saving text, but the spinner seems to be a bit more difficult. I'm using a Preference Connector that I found online that seems to work well for text, so that might be part of the problem. Here's a brief section of code that includes only the hair color selector spinner. I think my problem arises when I try to read the SP (readPerson()), but I'm not entirely sure. If you have any suggestions, please let me know. Thanks!

    public class PreferencesActivity extends Activity {
    Spinner hairSpinner; //hair color selector

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_profile);
        init();
    }
    private void init() {
          hairSpinner = (Spinner) findViewById(R.id.hair_spinner);
          ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                  this, R.array.hair_array, android.R.layout.simple_spinner_item);
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          hairSpinner.setAdapter(adapter);   
    }
    public void save(View view) {
        int hairText = hairSpinner.getSelectedItemPosition();

        if (hairText != 100)
            PreferenceConnector.writeInteger(this, PreferenceConnector.HAIR,
                    hairText);
    }
    private void readPerson() {
        hairSpinner.setSelection(getText(PreferenceConnector.readInteger(this,
                PreferenceConnector.HAIR, 0)));
    }
}

This is the part that I found online (a section of it):

    public class PreferenceConnector{
    public static final String PREF_NAME = "PEOPLE_PREFERENCES";
    public static final int MODE = Context.MODE_PRIVATE;

    public static final String HAIR = "HAIR";

    public static void writeInteger(Context context, String key, int value) {
        getEditor(context).putInt(key, value).commit();

    }

    public static int readInteger(Context context, String key, int defValue) {
        return getPreferences(context).getInt(key, defValue);
    }

    public static void writeString(Context context, String key, String value) {
        getEditor(context).putString(key, value).commit();

    }

    public static String readString(Context context, String key, String defValue) {
        return getPreferences(context).getString(key, defValue);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

晨曦÷微暖 2025-01-14 09:45:15
private void readPerson() {
    hairSpinner.setSelection(getText(PreferenceConnector.readInteger(this,
            PreferenceConnector.HAIR, 0)));
}

除了“getText”之外,一切看起来都很好,它到底在做什么?我猜 setSelection 正在寻找 int 但 getText 正在将其转换为 String 或其他东西。

private void readPerson() {
    hairSpinner.setSelection(getText(PreferenceConnector.readInteger(this,
            PreferenceConnector.HAIR, 0)));
}

Everything looks fine except for the "getText", what is it doing exactly? I'm guessing setSelection is looking for an int but getText is converting it to a String or something.

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