Question

Could someone maybe tell me what i'm doing wrong? I'm betting im missing one small thing. I've looked on the developer site and i've read some tutorials and i'm just not seeing what i did wrong.

I'm trying to use a ListPreference to decide which sound to play on a button click.

I have this at the top:

public String greensound;

Here's my OnClick code:

case R.id.green:
     SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);
     greensound  = prefs.getString("greensound", "gsone");
       if (greensound == "gsone") {
        mSoundManager.playSound(1); 
       } else if (greensound == "gstwo") {
        mSoundManager.playSound(2); 
       } else if (greensound == "gsthree") {
        mSoundManager.playSound(3);
       }
 break;

Here's my xml:

<ListPreference 
android:title="Geen Button" 
android:key="greensound"
android:summary="Select sound for the Green Button" 
android:entries="@array/green_list" 
android:entryValues="@array/green_list_values"
android:defaultValue="gsone">
</ListPreference>

here's my Settings.java:

package com.my.app;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);


    }


}

and here's my array's if that will help at all:

//This is the one I want to display to the user
    <string-array name="green_list"> 
      <item>Sound One</item>
      <item>Sound Two</item>
      <item>Sound Three</item>
      <item>Sound Four</item>
      <item>Sound Five</item>
    </string-array>


    <string-array name="green_list_values"> 
      <item>gsone</item>
      <item>gstwo</item>
      <item>gsthree</item>
      <item>gsfour</item>
      <item>gsfive</item>
    </string-array>

edit: added a logcat that kinda looked possibly related.

08-27 01:52:07.738: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}
08-27 01:52:07.748: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}
08-27 01:52:07.758: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}

DDMS > File Explorer > Data > Data > packageName > SharedPreferences This is what was in there:

com.my.app_preferences.xml:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="redsound">rsone</string>
<string name="greensound">gsone</string>
</map>

_has_set_default_values.xml:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="_has_set_default_values" value="true" />
</map>

This all really confuses me more because...It looks like greedsound does infact = gsone so.... I don't understand whats wrong its not even playing the default sound. and yes i've tested

mSoundManager.playSound(1); 
mSoundManager.playSound(2); 
mSoundManager.playSound(3);

all without the other code and they work great. I'm not sure what's work

Was it helpful?

Solution

greensound.equals("gsone")

OTHER TIPS

I had a similar problem. I changed my '==' comparisons to string.contentsEquals() and things started working. I eventually ended up putting the keys and values into HashMaps.

The only issue I can think of is that your preferences are not getting set before you are running your playSound code. To ensure the settings are loaded include the following code in your onCreate():

/* Loading default preferences the first time application is run */
        PreferenceManager.setDefaultValues(getApplicationContext(),
                R.xml.filename, false);

Also, check through the DDMS > File Explorer > Data > Data > packageName > SharedPreferences that your preferences are getting set.

When you are using Preference Activity and creating it from xml resource. It automatically creates a SharedPreference file: packageName_preferences (eg. com.my_company.my_app_preferences). Thus to access this you need to use the following code:

SharedPreferences prefs = getSharedPreferences("com.my.app_preferences", MODE_PRIVATE);

And finally remove the following line in the xml:

android:defaultValue="gsone"

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top