我如何在主类(Android)以外的其他类中获取我的首选项?
我的主类看起来像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将构造函数更改为如下所示:
然后将在类末尾声明文件名数组的方式更改为如下所示:
Change your constructor to look like this:
And then change how you're declaring the filenames array at the end of the class to look like this:
getDefaultSharedPreferences
等待的是Context
对象。因此,如果您遵循本教程,那么针对您的情况执行此操作的简单方法是:What
getDefaultSharedPreferences
is waiting for is aContext
object. So, the easy way to do it in your case, if you followed the tutorial is: