java - 无法从首选项(设置)中检索字符串

标签 java android sharedpreferences

我在我的 Android 应用程序中遇到了问题。问题是由于某种原因,我无法从我的 preferences.xml 文件中检索 ListPreference 的字符串。这是我试过的代码:

private void showUserSettings() {
    SharedPreferences sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this);

    try {

        String feet = sharedPrefs.getString("feet", "NULL");
        int intfeet = Integer.parseInt(feet);
        int intinches = Integer.parseInt(sharedPrefs.getString("feet", "NULL").trim());
        int weight = Integer.parseInt(sharedPrefs.getString("weight", "NULL").trim());
        int age = Integer.parseInt(sharedPrefs.getString("age", "NULL").trim());
        double calories;
        double inches = ((intfeet * 12) + intinches);
        if (gender.equals("Female")) {
            //BMR = 655 + ( 4.35 x weight in pounds ) + ( 4.7 x height in inches ) - ( 4.7 x age in years )
            calories = 655 + (4.35 * weight) + (4.7 * inches) - (4.7 * age);
        }
        else {
            //66 + ( 6.23 x weight in pounds ) + ( 12.7 x height in inches ) - ( 6.8 x age in years )
            calories = 66.5 + (6.23 * weight) + (12.7 * inches) - (6.8 * age);
        }
        //ListPreference listPreference = (ListPreference) findPreference("exercise");
        //CharSequence currText = listPreference.getEntry();
        String exercise = sharedPrefs.getString("exercise", "NULL").toString();
        if (exercise.equals("Sedentary")) {
            double sedentary = calories * 1.2;
            Toast.makeText(getApplicationContext(), sedentary + "", Toast.LENGTH_LONG).show();
        }
        if (exercise.equals("Light")){
            double light = calories * 1.375;
            Toast.makeText(getApplicationContext(), light + "", Toast.LENGTH_LONG).show();
        }
        if (exercise.equals("Moderate")){
            double moderate = calories * 1.55;
            Toast.makeText(getApplicationContext(), moderate + "", Toast.LENGTH_LONG).show();
        }
        if (exercise.equals("Very active")) {
            double veryactive = calories * 1.725;
            Toast.makeText(getApplicationContext(), veryactive + "", Toast.LENGTH_LONG).show();
        }
        if (exercise.equals("Extra active")) {
            double extraactive = calories * 1.9;
            Toast.makeText(getApplicationContext(), extraactive + "", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getApplicationContext(), "Uh oh", Toast.LENGTH_SHORT).show();
        }
    }catch (Exception e) {
        e.printStackTrace();
    }
}

每当它到达第二个 if/else 语句时,它直接转到 else 语句。我不知道为什么要这样做。我已经搜索过 SO 和谷歌,但无济于事。我基本上撞到了砖墙。非常感谢有关此问题的任何帮助。

最佳答案

您所有的练习条件都是独立的,else 仅与最后一个 if 语句相关联。使用 else if 以我相信您想要的方式将它们全部绑定(bind)在一起。

关于java - 无法从首选项(设置)中检索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863806/

相关文章:

android - 在 Activity 之间使用共享偏好的最佳方式是什么

java - 构造函数 Intent(DataCountUtilities, Class<DataCountService>) 未定义

java - 使用 NIO DatagramChannel 是否需要处理部分读取/写入的数据包?

java - 运行时从Jar文件加载资源

android - 如何在 Android SQLite 和 SQL Server 之间进行双向同步

android - 如何在不删除其 _ID 列值的情况下删除行?

java - WatchService 排除文件夹

java - 从 Activity 中的 Fragment 单击返回会显示空 Activity

android - 如何在 Android 中捕获 webview 到位图?

Android SharedPreferences 限制