1

我用 OnItemSelectedListener 函数做了一个微调器。我从一个包含 4 个选项的数组中创建它:

        String[] arrayOfString = new String[4];
    arrayOfString[0] = "Kiskunfélegyháza HQ [160kbps MP3]";
    arrayOfString[1] = "Kiskunfélegyháza MQ [64kbps AAC]";
    arrayOfString[2] = "Kiskunmajsa HQ [160kbps MP3]";
    arrayOfString[3] = "Kiskunmajsa MQ [64kbps AAC]";
    this.items = arrayOfString;

加载数据中:

        PreferenceManager.setDefaultValues(this, 0x7f040000, false);
    final SharedPreferences localSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    this.mWebView_enabled = localSharedPreferences.getBoolean("mWebView_enabled", true);
    this.savszelesseg_port = localSharedPreferences.getString("savszelesseg_mentes", "savszelesseg_port");
    this.savszelesseg_saved = this.savszelesseg_port;

           final Spinner localSpinner = (Spinner)findViewById(R.id.savszelesseg);
    ArrayAdapter localArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, this.items);
    localArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    localSpinner.setAdapter(localArrayAdapter);

如果有人选择一个选项,它将被保存到 SharedPreferencies 并更改音乐。但是,当有人退出然后重新打开应用程序时,如何取回数据?

我将它保存为 savszelesseg_mentes,并保存 savszelesseg_port。我还有一个选项菜单来保存此设置。

    localSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
    {
        public void onItemSelected(AdapterView parentView, View paramView, int paramInt, long paramLong) 
        {
            if (!savszelesseg_port_modositva)
            {
              savszelesseg_port = localSharedPreferences.getString("savszelesseg_mentes", "savszelesseg_port");
              savszelesseg_port_modositva = true;

            }
            savszelesseg_beallitas = items[paramInt];

            if (savszelesseg_beallitas == "Kiskunfélegyháza HQ [160kbps MP3]")
            {
              savszelesseg_port = "8500/relay_911";
              SharedPreferences.Editor editor = localSharedPreferences.edit();
              editor.putString("savszelesseg_mentes", savszelesseg_port);
              editor.commit();
            }
            if (savszelesseg_beallitas == "Kiskunfélegyháza MQ [64kbps AAC]")
            {
              savszelesseg_port = "8500/relay_mobile_911";
              SharedPreferences.Editor editor = localSharedPreferences.edit();
              editor.putString("savszelesseg_mentes", savszelesseg_port);
              editor.commit();
            }
            if (savszelesseg_beallitas == "Kiskunmajsa HQ [160kbps MP3]")
            {
              savszelesseg_port = "8500/relay_882";
              SharedPreferences.Editor editor = localSharedPreferences.edit();
              editor.putString("savszelesseg_mentes", savszelesseg_port);
              editor.commit();
            }
            if (savszelesseg_beallitas == "Kiskunmajsa MQ [64kbps AAC]")
            {
             savszelesseg_port = "8500/relay_mobile_882";
             SharedPreferences.Editor editor = localSharedPreferences.edit();
             editor.putString("savszelesseg_mentes", savszelesseg_port);
             editor.commit(); 
            }




            if (lejatszas == 1 && savszelesseg_port_modositva)
            {

                  stopPlaying();
                  startPlaying();

            }

            if (lejatszas == 0 && savszelesseg_port_modositva == true)
            {
              startPlaying();
            }


        }

如果没有选择:

        public void onNothingSelected(AdapterView parentView) 
        {
        }
4

2 回答 2

2

您正在比较字符串而不是引用,所以使用 equals() 方法不要使用 ==

您正在使用

if (savszelesseg_beallitas == "Kiskunfélegyháza HQ [160kbps MP3]")

改为使用

if (savszelesseg_beallitas.equals("Kiskunfélegyháza HQ [160kbps MP3]"))

或者

if ("Kiskunfélegyháza HQ [160kbps MP3]".equals(savszelesseg_beallitas));

即使评估为真,请参阅If 语句不会执行

于 2012-09-03T11:44:37.790 回答
0

像这样,您应该这样做以获取数据,然后设置正确的选择:

int index = 0;
String savedString = localSharedPreferences.getString("savszelesseg_mentes", "");
for(int i = 0; i < arrayOfString.length(); i++){
    if(savedString.equals(arrayOfString[i])){
        index = i;
        break;
    }
}
localSpinner.setSelection(index, true);
于 2012-09-03T11:56:41.893 回答