Can you check my code for anything missing?
I'm trying to get use the value from a listpreference in an if statement but the if statement never responds to the value stored in the listpreference.
Here the coding I'm using:
String stringChimeVolume = clockSettings.getString("ChimeVolume",
"Default");
Toast.makeText(context, "The preference is: " + stringChimeVolume,
Toast.LENGTH_SHORT).show();
if (stringChimeVolume == "2") {
manager.setStreamVolume(AudioManager.STREAM_MUSIC,
2, AudioManager.FLAG_VIBRATE);
Toast.makeText(context, "The volume is at 2: ",
Toast.LENGTH_SHORT).show();
} else if (stringChimeVolume == "3") {
Toast.makeText(context, "The volume is at 3: ",
Toast.LENGTH_SHORT).show();
manager.setStreamVolume(AudioManager.STREAM_MUSIC,
7, AudioManager.FLAG_VIBRATE);
}
else if (stringChimeVolume == "4") {
manager.setStreamVolume(AudioManager.STREAM_MUSIC,
15, AudioManager.FLAG_VIBRATE);
}
I used the first Toast statement to ensure a value was there. It has the value of 2 but the Toast in the if statement for "2" is not executing.
Here is the arrays.xml I used for the listpreference:
<string-array name="chimeVolumeLabels">
<item>Use System Volume</item>
<item>Emad</item>
<item>Medium</item>
<item>Loud</item>
</string-array>
<string-array name="chimeVolumeValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
This is the settings.xml with the listpreference:
<PreferenceCategory android:title="Media:">
<CheckBoxPreference android:key="ChimeWhenMusicIsPlaying"
android:title="@string/ChimeWhenMusicIsPlayingTitle" android:summary="@string/ChimeWhenMusicIsPlayingSummary"
android:defaultValue="false" />
<ListPreference android:title="Chime Volume"
android:key="ChimeVolume" android:summary="Select volume for the chiming sound."
android:entries="@array/chimeVolumeLabels" android:entryValues="@array/chimeVolumeValues"
android:defaultValue="1" />
</PreferenceCategory>
All help will be greatly appreciated.
Truly, Emad
use .equals instead of == since == compare references not the actual value
like
if (stringChimeVolume.equals( "2")) instead of
if (stringChimeVolume== "2")