2012-10-25 28 views
0

這是(的一部分)我arrayValues:提交和應用不會改變sharedPrefs

<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!

回答

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; 
} 
+0

thx!沒關係!!! –