views:

105

answers:

1

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>

//I also tried removing the default value here it didnt seem to change anything but, should it be removed?

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>

Here's 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}

For those that might ask here's my 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 greensound does == gsone so.... I don't understand whats wrong its not even playing the default sound. and yes i've tested all this code without the listpreference code and they work great. I'm not sure what's wrong

mSoundManager.playSound(1); 
mSoundManager.playSound(2); 
mSoundManager.playSound(3);
+1  A: 

Change greensound == "gsone" to greensound.equals(gsone)

This is a very common error for new java programmers and people who don't have a good grasp on object oriented concepts. Here's a good references for comparing objects and values in java

http://leepoint.net/notes-java/data/expressions/22compareobjects.html


Also, why does everyone insist on implementing onClickListener in the main activity? Is there some new tutorial that suggests this or something?

instead do an anonymous inner class (unless you have a bunch of buttons and you want to set their click listeners programatically.



Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new onClickListener(){

    @Override
    public void onClick(){
        Preferences pref = MyActivity.this.getSharedPreferences()
        String s = pref.getString("sound","gsone");
}
});


Falmarri
greensound.equals("gsone") was IT! THANK YOU i was about to break my phone!!!!!!!!!!!!!!! edit:the site wont let me mark it as an answer for another 3 min so ill do it when i can. thanks!
brybam