简单的 ListPreference 不起作用。有什么想法吗?

发布于 2024-09-16 08:30:22 字数 3132 浏览 3 评论 0原文

有人可以告诉我我做错了什么吗?我敢打赌我错过了一件小事。我查看了开发人员网站并阅读了一些教程,但我只是没有看到我做错了什么。

我正在尝试使用 ListPreference 来决定单击按钮时播放哪种声音。

我在顶部有这个:

public String greensound;

这是我的 OnClick 代码:

case R.id.green:
     SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);
     greensound  = prefs.getString("greensound", "gsone");
       if (greensound == "gsone") {
        mSoundManager.playSound(1); 
       } else if (greensound == "gstwo") {
        mSoundManager.playSound(2); 
       } else if (greensound == "gsthree") {
        mSoundManager.playSound(3);
       }
 break;

这是我的 xml:

<ListPreference 
android:title="Geen Button" 
android:key="greensound"
android:summary="Select sound for the Green Button" 
android:entries="@array/green_list" 
android:entryValues="@array/green_list_values"
android:defaultValue="gsone">
</ListPreference>

这是我的 Settings.java:

package com.my.app;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);


    }


}

这是我的数组,如果这有帮助的话:

//This is the one I want to display to the user
    <string-array name="green_list"> 
      <item>Sound One</item>
      <item>Sound Two</item>
      <item>Sound Three</item>
      <item>Sound Four</item>
      <item>Sound Five</item>
    </string-array>


    <string-array name="green_list_values"> 
      <item>gsone</item>
      <item>gstwo</item>
      <item>gsthree</item>
      <item>gsfour</item>
      <item>gsfive</item>
    </string-array>

编辑: 添加了一个看起来可能相关的 logcat。

08-27 01:52:07.738: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}
08-27 01:52:07.748: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}
08-27 01:52:07.758: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}

DDMS>文件资源管理器>数据>数据>包名>共享首选项 这就是其中的内容:

com.my.app_preferences.xml:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="redsound">rsone</string>
<string name="greensound">gsone</string>
</map>

_has_set_default_values.xml:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="_has_set_default_values" value="true" />
</map>

这一切真的让我更加困惑,因为......看起来贪婪的声音实际上= gsone 所以......我不明白出了什么问题,它甚至没有播放默认声音。是的,我已经

mSoundManager.playSound(1); 
mSoundManager.playSound(2); 
mSoundManager.playSound(3);

在没有其他代码的情况下测试了所有内容,并且它们工作得很好。我不确定什么是工作

Could someone maybe tell me what i'm doing wrong? I'm betting im missing one small thing. I've looked on the developer site and i've read some tutorials and i'm just not seeing what i did wrong.

I'm trying to use a ListPreference to decide which sound to play on a button click.

I have this at the top:

public String greensound;

Here's my OnClick code:

case R.id.green:
     SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);
     greensound  = prefs.getString("greensound", "gsone");
       if (greensound == "gsone") {
        mSoundManager.playSound(1); 
       } else if (greensound == "gstwo") {
        mSoundManager.playSound(2); 
       } else if (greensound == "gsthree") {
        mSoundManager.playSound(3);
       }
 break;

Here's my xml:

<ListPreference 
android:title="Geen Button" 
android:key="greensound"
android:summary="Select sound for the Green Button" 
android:entries="@array/green_list" 
android:entryValues="@array/green_list_values"
android:defaultValue="gsone">
</ListPreference>

here's my Settings.java:

package com.my.app;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);


    }


}

and here's my array's if that will help at all:

//This is the one I want to display to the user
    <string-array name="green_list"> 
      <item>Sound One</item>
      <item>Sound Two</item>
      <item>Sound Three</item>
      <item>Sound Four</item>
      <item>Sound Five</item>
    </string-array>


    <string-array name="green_list_values"> 
      <item>gsone</item>
      <item>gstwo</item>
      <item>gsthree</item>
      <item>gsfour</item>
      <item>gsfive</item>
    </string-array>

edit: added a logcat that kinda looked possibly related.

08-27 01:52:07.738: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}
08-27 01:52:07.748: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}
08-27 01:52:07.758: WARN/Resources(6846): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f090000}

DDMS > File Explorer > Data > Data > packageName > SharedPreferences
This is what was in there:

com.my.app_preferences.xml:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="redsound">rsone</string>
<string name="greensound">gsone</string>
</map>

_has_set_default_values.xml:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="_has_set_default_values" value="true" />
</map>

This all really confuses me more because...It looks like greedsound does infact = gsone
so.... I don't understand whats wrong its not even playing the default sound. and yes i've tested

mSoundManager.playSound(1); 
mSoundManager.playSound(2); 
mSoundManager.playSound(3);

all without the other code and they work great. I'm not sure what's work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角卖回忆 2024-09-23 08:30:22

greensound.equals(“gsone”)

greensound.equals("gsone")

撩发小公举 2024-09-23 08:30:22

我有类似的问题。我将“==”比较更改为 string.contentsEquals() 并且事情开始工作。我最终将键和值放入 HashMap 中。

I had a similar problem. I changed my '==' comparisons to string.contentsEquals() and things started working. I eventually ended up putting the keys and values into HashMaps.

躲猫猫 2024-09-23 08:30:22

我能想到的唯一问题是,在运行 playSound 代码之前,您的首选项尚未设置。为了确保加载设置,请在 onCreate() 中包含以下代码:

/* Loading default preferences the first time application is run */
        PreferenceManager.setDefaultValues(getApplicationContext(),
                R.xml.filename, false);

此外,请检查 DDMS >文件资源管理器>数据>数据> packageName > > SharedPreferences 表示您的首选项正在设置。

当您使用首选项活动并从 xml 资源创建它时。它会自动创建一个 SharedPreference 文件:packageName_preferences(例如 com.my_company.my_app_preferences)。因此,要访问它,您需要使用以下代码:

SharedPreferences prefs = getSharedPreferences("com.my.app_preferences", MODE_PRIVATE);

最后删除 xml 中的以下行:

android:defaultValue="gsone"

希望这会有所帮助。

The only issue I can think of is that your preferences are not getting set before you are running your playSound code. To ensure the settings are loaded include the following code in your onCreate():

/* Loading default preferences the first time application is run */
        PreferenceManager.setDefaultValues(getApplicationContext(),
                R.xml.filename, false);

Also, check through the DDMS > File Explorer > Data > Data > packageName > SharedPreferences that your preferences are getting set.

When you are using Preference Activity and creating it from xml resource. It automatically creates a SharedPreference file: packageName_preferences (eg. com.my_company.my_app_preferences). Thus to access this you need to use the following code:

SharedPreferences prefs = getSharedPreferences("com.my.app_preferences", MODE_PRIVATE);

And finally remove the following line in the xml:

android:defaultValue="gsone"

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文