English 中文(简体)
Android: PreferenceActivity: Why is this simple logic check on a stored preference value not evaluating?
原标题:

Following an example from Professional Android 2 Application Development I set up a pref class as follows:

//Preferences.java
public class Preferences extends PreferenceActivity{
    public static final String PREF_SPEED_UNIT ="PREF_SPEED_UNIT";
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.userpreferences);
    }  
}

In my activity class:

// main.java 

private static final int SHOW_PREFERENCES = 1;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SHOW_PREFERENCES)
        updateFromPreferences();
        if (resultCode == Activity.RESULT_OK) {
        }   
}

private void updateFromPreferences() {
    Log.d(TAG, "updateFromPreferences()");
    Context context = getApplicationContext();
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(context);
    // Set the SpeedMode string using object reference to custom view
    mSpeedMode = prefs.getString(Preferences.PREF_SPEED_UNIT, "MPH");
    speedview.SpeedMode = mSpeedMode;  
}
// Using have a Button view that pops the options for testing
private OnClickListener optionbuttonCL = new OnClickListener() {
    public void onClick(View v) {
        Context context = getApplicationContext();
        Intent i = new Intent(context, Preferences.class);
        startActivityForResult(i, SHOW_PREFERENCES);
    }
};

The issue is here:

    // SpeedView.java
    if (SpeedMode == "MPH"){
        // set speed in miles/hour
    }
    if (SpeedMode == "KPH"){
        //set speed in kilometers/hour
    }

It works, however if I switch speed modes to KPH (default is MPH), the display no longer updates.

The bewildering thing is even if SpeedMode is shown to be "MPH" or "KPH" using the Eclipse debugger, the if statements will not execute.

Here is the rest of the code:

//userpreferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:key="PREF_SPEED_UNIT"
    android:title="Speed Unit" 
    android:summary="Choose between MPH or KPH"
    android:entries="@array/speed_options" 
    android:entryValues="@array/speed_values"
    android:dialogTitle="Speed Unitz" 
    android:defaultValue="MPH" />
</PreferenceScreen>


// arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="speed_options">
    <item>MPH</item>
    <item>KPH</item>
</string-array>
<string-array name="speed_values">
    <item>MPH</item>
    <item>KPH</item>
</string-array>

Thanks

最佳回答

What is speedview.SpeedMode? Where is it declared. I ll bet your problem is

SpeedMode == "MPH"

Change it to

Speedmode.equals("MPH");

Also, by convention in java and languages that use camelCase, variables should be lowercase.

By variables being lowercase, I mean the first letter of the variable. So if you had a class MyClass. You would declare it

MyClass myClass;

That way you know you re not trying to access class methods instead of instance methods.

This is a good explanation on why you need to use .equals() instead of ==

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

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签