我如何在主类(Android)以外的其他类中获取我的首选项?

发布于 2024-10-05 05:22:34 字数 1204 浏览 4 评论 0原文

我的主类看起来像这样:

public class Soundboard extends Activity 
{ 
    SharedPreferences preferences;

 @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 


     // Initialize preferences
        preferences = PreferenceManager.getDefaultSharedPreferences(this);

}

private void loadButtonText() {
        startPlayerBtn.setText(preferences.getString("buttontext1", "n/a"));

}

我正在工作,但我用 ButtonAdapter (额外类),但在那里找不到我的首选项。我你看看链接我有一个在 ButtonAdapter.java 中看起来像这样的数组:

 public String[] filesnames = {
             "Text button 1",
            "Text button 2",  
            "Text button 3"
            };

我怎样才能将我的设置放入数组中?像这样的东西不起作用:

     public String[] filesnames = {
            preferences.getString("buttontext1", "n/a",
            preferences.getString("buttontext2", "n/a",  
            preferences.getString("buttontext3", "n/a"
            };

请帮助我,我真的被困住了。

My main class look like this:

public class Soundboard extends Activity 
{ 
    SharedPreferences preferences;

 @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 


     // Initialize preferences
        preferences = PreferenceManager.getDefaultSharedPreferences(this);

}

private void loadButtonText() {
        startPlayerBtn.setText(preferences.getString("buttontext1", "n/a"));

}

This i workning but I have change my code a litte bit with a ButtonAdapter (extra class) but cant find my Preferences there. I you look att the link I have an array that look like this in ButtonAdapter.java:

 public String[] filesnames = {
             "Text button 1",
            "Text button 2",  
            "Text button 3"
            };

How can I but my settings into the array? Something like this that is not working:

     public String[] filesnames = {
            preferences.getString("buttontext1", "n/a",
            preferences.getString("buttontext2", "n/a",  
            preferences.getString("buttontext3", "n/a"
            };

Please help me, I am really stuck.

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

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

发布评论

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

评论(2

愿与i 2024-10-12 05:22:34

将构造函数更改为如下所示:

public ButtonAdapter(Context c) {  
    preferences = PreferenceManager.getDefaultSharedPreferences(c);
    filenames = new String[] {
        preferences.getString("buttontext1", "n/a"),
        preferences.getString("buttontext2", "n/a")
    }
}

然后将在类末尾声明文件名数组的方式更改为如下所示:

public String[] filenames;

Change your constructor to look like this:

public ButtonAdapter(Context c) {  
    preferences = PreferenceManager.getDefaultSharedPreferences(c);
    filenames = new String[] {
        preferences.getString("buttontext1", "n/a"),
        preferences.getString("buttontext2", "n/a")
    }
}

And then change how you're declaring the filenames array at the end of the class to look like this:

public String[] filenames;
当爱已成负担 2024-10-12 05:22:34

getDefaultSharedPreferences 等待的是 Context 对象。因此,如果您遵循本教程,那么针对您的情况执行此操作的简单方法是:

preferences = PreferenceManager.getDefaultSharedPreferences(mContext);

What getDefaultSharedPreferences is waiting for is a Context object. So, the easy way to do it in your case, if you followed the tutorial is:

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