views:

112

answers:

3

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

A: 

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.

Sameer Segal
Thank you for the attempt, but sadly it's not working. I updated my first post with the entire code i'm currently using. Does anything need to be added to Settings.java? For example i know there is some code i need to add to Settings.java when i use checkboxes.
brybam
Figured it out -- check out my updated answer! Sorry, I couldn't see this immediately. Now it should definitely work!
Sameer Segal
It's still not working. I updated my post again with what i'm using. I also tried com.my.app_Settings since that's the name of my pref java file. Are you sure nothing goes in the Settings.java?
brybam
No, don't worry about settings.java. Use debugger to see the value greensound; Use DDMS and File Explorer to find out if the file is getting set or not?
Sameer Segal
ALright, it looks like for some reason i can only get DDMS to work with the emulator. It wont work with my nexus one. anyway, i'll update the orginal post with the information i found on the shared prefs.
brybam
Something interesting i've noticed is if i have the main code the way it is above (the way that should be right) NOTHING happens when the button is pressed. But lets say i change something like greensound = prefs.getString("greensound", "gsone"); to greensound = prefs.getString("blah blah blah", "gsone"); the mSoundManager.playSound(1); plays and its the only thing that will play regardless of the setting.
brybam
Thats a security feature. You cannot use DDMS for a device.The sound plays because its picking up the default value thats getting entered. Please use DDMS > FileExplorer and see what is getting set in the preference file.
Sameer Segal
Updated my answer -- drop the default value clause. It will any case pick the first item.
Sameer Segal
thanks for still trying, but sadly again that did not work
brybam
Sameer Segal
yeah i got that posted up there under the logcat. It looks like its right to me, maybe it'll make sense to you
brybam
Thought you might want to know the answer is greensound.equals("gsone") i cant believe it was so simple!
brybam
Damn! I make this mistake all the time too. Coming from a C++ background it really seems very counter intuitive.
Sameer Segal
A: 

greensound.equals("gsone")

brybam
A: 

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.

DarthNoodles