0

这是我的arrayValues(的一部分):

<string-array name="securityValues">
<item name="wep">"capabilities LIKE '%WEP%'"</item>
<item name="wpa">"capabilities LIKE '%WPA%'"</item>
<item name="free">"capabilities NOT LIKE '%WPA%' AND capabilities NOT LIKE '%WEP%' AND capabilities NOT LIKE '%WPS%'"</item>
<item name="all">"1"</item>

这是我的preferences.xml的一部分

 <ListPreference 
    android:key="visualizzazione_marker_security"
    android:title="Sicurezza Wifi da visualizzare"
    android:summary="Scegli con quale protocollo di sicurezza vuoi che siano visualizzate le wifi"
    android:entries="@array/security"
    android:entryValues="@array/securityValues"

/> 

最后是我的代码:

    @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, 0, 0, "Only free wifi");
    menu.add(Menu.NONE, 1, 1, "All wifi");
    return super.onCreateOptionsMenu(menu);
  }

switch (item.getItemId()) {
    case 0:

        Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
        edit.putString("visualizzazione_marker_security","capabilities NOT LIKE \'%WPA%\' AND capabilities NOT LIKE \'%WEP%\' AND capabilities NOT LIKE \'%WPS%\'").apply();
        edit.commit();
        Toast.makeText(this, "Wait 30 seconds please", Toast.LENGTH_LONG).show();
        case 1:
            Editor edit1 = PreferenceManager.getDefaultSharedPreferences(this).edit();
        edit1.putString("visualizzazione_marker_security","1").apply();
        edit1.commit();
        Toast.makeText(this, "Wait 30 seconds please", Toast.LENGTH_LONG).show();
        return true;
}

为什么case0不应用修改?首选项中的值始终更改为 1!

4

1 回答 1

2

您的案例块中缺少一条break;语句。所以案例 0 被案例 1 覆盖。

switch (item.getItemId()) {
    case 0:
        Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
        edit.putString("visualizzazione_marker_security","capabilities NOT LIKE \'%WPA%\' AND capabilities NOT LIKE \'%WEP%\' AND capabilities NOT LIKE \'%WPS%\'").apply();
        edit.commit();
        Toast.makeText(this, "Wait 30 seconds please", Toast.LENGTH_LONG).show();
        break;

    case 1:
        Editor edit1 = PreferenceManager.getDefaultSharedPreferences(this).edit();
        edit1.putString("visualizzazione_marker_security","1").apply();
        edit1.commit();
        Toast.makeText(this, "Wait 30 seconds please", Toast.LENGTH_LONG).show();
        return true;
}
于 2012-10-25T10:39:51.980 回答