-2

I've got a strange error. I'm comparing preference changes when the preference changes.

When a preference changes it gives me a string of the key that's changed.

I've made a list of IF statments to check which statement changes and to update that current preference (performance etc etc, and since we can't use sharedprefs onDestory we have to do one at a time..)

Anyhow the other keys are working but this one seems to fail at the end. Here:

First of all we have the XML: (PS this function changes the size of the bitmap within the app)

This is within the values.xml

<string-array name="itemSize">
    <item>Small</item>
    <item>Medium</item>
    <item>Large</item>
</string-array>
<string-array name="itemSizeNumbers">
    <item>8</item>
    <item>12</item>
    <item>18</item>
</string-array>

This is within the settings.xml

<ListPreference
    android:title="Item Size"
    android:summary="Set the Item size."
    android:key="ItemSize"
    android:defaultValue="Medium"
    android:entries="@array/ItemSize"
    android:entryValues="@array/ItemSizeNumbers"
/>

When the preference is changed this function is called:

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
    {
        if(started)
        {
            _battlefield.upDateValues(key);
        }
    }

That passes onto: (Probably where the problem is)

public void upDateValues(String key)
{
            //This returns ItemSize on item size change.
    Log.w("myapp", key);
    String nkey = key;

    if(nkey == "Items")
    {
                    //This one works
        this._no_of_Items = this.prefs.getInt("Items", 4);
    }

    if(nkey == "respawn")
    {
                    //This one works
        this._respawn_time = this.prefs.getInt("respawn", 1);
    }

            //The fail zone.
    if(nkey == "ItemSize")
    {
                    //This one does not work! Key outputs ItemSize ????
        Log.w("myapp", "Item size activated");

        this._items_screen_percentage = this.prefs.getInt("ItemSize", 10);

        this.initializeBitmaps();

        //this.initialize(_context, _surfaceHolder, prefs);
    }
}
Oliver Dixon
  • 7,012
  • 5
  • 61
  • 95
  • 4
    And cue the one millionth question `Why does comparing strings with ==` fail. I mean it's not as if it were the first point in the `Frequently Asked Questions` list of this site.. oh wait – Voo Feb 08 '12 at 21:06
  • It though shared preferences might have some werid array with both XML vars coming from it. – Oliver Dixon Feb 08 '12 at 21:14

1 Answers1

1

you should use the equals method for comparing the content of String.

if (str1.equals(str2)) {
       ...
}

take a look here:

http://www.zparacha.com/java-string-comparison/

Sam Felix
  • 1,329
  • 1
  • 10
  • 23
  • I do indeed. I've just read up on what's the difference. "==" is reference a memory location, equals is the actual text. (For anyone else who has the same problem) – Oliver Dixon Feb 08 '12 at 21:21