简体   繁体   English

由于某种原因,我的if语句没有触发

[英]For some reason my if statement isn't triggering

I have created an onSharedPreferencesChangeListener which is working correctly. 我创建了一个正常工作的onSharedPreferencesChangeListener。 I know this because when I setup the listener I logged the listener and it is triggering on a change. 我知道这一点,因为当我设置监听器时,我记录了监听器并且它正在触发更改。

However, I was getting an error when I was trying to switch the key variable (saying that I need JRE 1.7+ in order to switch a string), so I changed it to if-elseif statements. 但是,当我尝试切换密钥变量时(我说我需要JRE 1.7+才能切换字符串)时出现错误,因此我将其更改为if-elseif语句。

The if-elseif statements weren't triggering so I changed the first two to if statements and put the elseif after that (thinking that the elseif might have been the problem). if-elseif语句没有触发,所以我将前两个更改为if语句并在之后放入elseif(认为elseif可能是问题)。 Still no triggering. 仍然没有触发。

The weird thing is that when I logged the event trigger, I output " "+key+" " as the string and the key value is the exact strings that I'm testing for. 奇怪的是,当我记录事件触发器时,我输出“ ”+ key +“ ”作为字符串,键值是我正在测试的确切字符串。

Here is my code, can someone point me in the right direction please? 这是我的代码,有人能指出我正确的方向吗?

    // logic for action when a shared preference is changed.
    prefs.registerOnSharedPreferenceChangeListener( new SharedPreferences.OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            String TAG = "UltimateScoreboard";

            if (key == "Main_Clock_Minutes") {
                msMainClockStart = Long.valueOf( prefs.getString( "Main_Clock_Minutes", "0" ) ) * 60000;
                Log.i( TAG, key + " changed: " + String.valueOf(msMainClockStart) );
            }

            if ( key == "Use_ShotClock" ) {
                useShotClock = prefs.getBoolean( "Use_ShotClock", false );

                if( useShotClock )
                    btnShotClock.setVisibility( View.VISIBLE );
                else
                    btnShotClock.setVisibility( View.INVISIBLE );

                Log.i( TAG, key );
            } 

            else if ( key == "Shot_Clock_Seconds" ) {
                msShotClockStart = Long.valueOf( prefs.getString( "Shot_Clock_Seconds", "0" ) ) * 1000;
                Log.i( TAG, key + " changed: " + Long.valueOf(msShotClockStart) );
            }

You must use the String.equals() function to compare strings. 您必须使用String.equals()函数来比较字符串。 == with Strings only compares memory locations. == with Strings仅比较内存位置。

For example: if (key.equals("Main_Clock_Minutes")) 例如: if (key.equals("Main_Clock_Minutes"))

You can not compare strings using == . 您无法使用==比较字符串。 Use the String class methods equals() or equalsIgnoreCase() . 使用String类方法equals()equalsIgnoreCase()

if ( key.equals("Use_ShotClock") ) { }

This is how you compare strings 这是比较字符串的方式

you should compare like if ( key.equals("Use_ShotClock")) or if ( key.equalsIgnoreCase("Use_ShotClock")). 你应该比较if(key.equals(“Use_ShotClock”))或if(key.equalsIgnoreCase(“Use_ShotClock”))。 because string are objects in java. 因为string是java中的对象。 so if you compare like if ( key == "Use_ShotClock" ) it will try to compare the referances of those 2 string objects which will be different. 因此,如果您比较if(key ==“Use_ShotClock”),它将尝试比较那两个不同的字符串对象的参考。

Have you tried using .equals() in comparing strings ? 您是否尝试过使用.equals()来比较strings

Like this: 像这样:

if (key.equals("Main_Clock_Minutes")) {
    msMainClockStart = Long.valueOf( prefs.getString( "Main_Clock_Minutes", "0" ) ) * 60000;
    Log.i( TAG, key + " changed: " + String.valueOf(msMainClockStart) );
} else if ( key.equals("Use_ShotClock")) {
    useShotClock = prefs.getBoolean( "Use_ShotClock", false );

    if( useShotClock )
        btnShotClock.setVisibility( View.VISIBLE );
    else
        btnShotClock.setVisibility( View.INVISIBLE );

    Log.i( TAG, key );
} else if ( key.equals("Shot_Clock_Seconds")) {
    msShotClockStart = Long.valueOf( prefs.getString( "Shot_Clock_Seconds", "0" ) ) * 1000;
    Log.i( TAG, key + " changed: " + Long.valueOf(msShotClockStart) );
}

Strings have to be tested on equality with String's method equals(String s). 必须使用String的方法equals(String s)对字符串进行相等性测试。

The "==" Operator hasn't been overloaded for Strings and because a String is a reference Type (it's an object, not a primitive type) it's the memory address which is compared using "==". “==”运算符没有为Strings重载,因为String是引用类型(它是一个对象,而不是一个基本类型),它是使用“==”进行比较的内存地址。 Because of Javas ability to share memory for multiple variables, there is a chance to get true when comparing strings with "==", but that's less probable and very unpredictable. 由于Javas能够为多个变量共享内存,因此在将字符串与“==”进行比较时,有可能实现,但这种可能性较小且非常不可预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM