字符串比较失败 - 共享首选项键
我有一个奇怪的错误。我正在比较偏好变化时的偏好变化。
当首选项更改时,它会给我一串已更改的密钥。
我制作了一份 IF 语句列表,以检查哪些语句发生了更改并更新当前首选项(性能等,并且由于我们不能使用共享首选项 onDestory,所以我们必须一次执行一个..)
无论如何,其他键是工作,但这似乎最终失败了。这里:
首先,我们有 XML:(PS 这个函数会更改应用程序内位图的大小)
这是在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>
这是在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"
/>
当首选项更改时,将调用此函数
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if(started)
{
_battlefield.upDateValues(key);
}
}
:到:(可能问题出在哪里)
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 equals 方法来比较 String 的内容。
看看这里:
http://www.zparacha.com/java-string-comparison/< /a>
you should use the equals method for comparing the content of String.
take a look here:
http://www.zparacha.com/java-string-comparison/