创建共享preferences内片段对象 [英] Creating SharedPreferences object inside Fragment

查看:181
本文介绍了创建共享preferences内片段对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发小型应用程序,为我的队友类,这是调整的时间表。它使用片段显示一周的每一天作为一个不错的,用手指滑动式UI:

在SettingsActivity.class有(定义在XML中)preferences,它可以自动存储在共享preferences设置。问题是类,在这种片段被定义,是静态的。我不能使用有非静态方法的参考,像一个:

 共享preferences设置= getShared preferences(APP_ preFERENCES,MODE_PRIVATE);

通过对developer.android.com阅读文档和使用Google的帮助下,我发现,使用由SettingsActivity.class创建的共享preferences,我必须使用的 preferenceManager ,这样的:

 共享preferences设置= preferenceManager.getDefaultShared preferences(本);

现在它不告诉我关于非静态引用错误,但我不知道如何引用SettingsActivity.class在getDefaultShared preferences的说法,因为我在片段静态类很,所以我不能用这个。

我也试图创建静态类以外的共享preference对象。但是,该对象的所有使用抱怨说:非静态字段不能从静态上下文引用。

这是很重要的,我使用的共享preferences那里,因为后来我要去实现的教训(TextViews)颜色根据一个小时发生变化,而且也切换,能够设置。

下面是片段类的code:

 公共静态类Dzien扩展片段{    公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;    公共Dzien(){
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);
        TextView的lekcja1 =(TextView中)rootView.findViewById(R.id.lekcja1);
        TextView的lekcja2 =(TextView中)rootView.findViewById(R.id.lekcja2);
        TextView的lekcja3 =(TextView中)rootView.findViewById(R.id.lekcja3);
        TextView的lekcja4 =(TextView中)rootView.findViewById(R.id.lekcja4);
        TextView的lekcja5 =(TextView中)rootView.findViewById(R.id.lekcja5);
        TextView的lekcja6 =(TextView中)rootView.findViewById(R.id.lekcja6);
        TextView的lekcja7 =(TextView中)rootView.findViewById(R.id.lekcja7);
        TextView的lekcja8 =(TextView中)rootView.findViewById(R.id.lekcja8);
        TextView的lekcja9 =(TextView中)rootView.findViewById(R.id.lekcja9);
        //这里是我尝试创建共享preference对象
        共享preferences设置= preferenceManager.getDefaultShared preferences(本);
        开关(getArguments()。调用getInt(ARG_SECTION_NUMBER)){
            案例1:{//Poniedziałek
                lekcja1.setText(的getString(R.string.wos));
                lekcja2.setText(的getString(R.string.mat));
                lekcja3.setText(的getString(R.string.ang));
                lekcja4.setText(的getString(R.string.gw));
                lekcja5.setText(的getString(R.string.his));
                lekcja6.setText(的getString(R.string.wf));
                lekcja7.setText(的getString(R.string.pp));
                如果(settings.getInt(GRUPA_INFORMATYKA,1)== 1){
                    lekcja8.setText(的getString(R.string.inf));
                }其他{
                    lekcja8.setText(的getString(R.string.brak));
                }
                lekcja9.setText(的getString(R.string.brak));
                打破;
            }
            案例2:{// Wtorek
                lekcja1.setText(的getString(R.string.mat));
                lekcja2.setText(的getString(R.string.pp));
                lekcja3.setText(的getString(R.string.rel));
                lekcja4.setText(的getString(R.string.wf));
                lekcja5.setText(的getString(R.string.pol));
                lekcja6.setText(的getString(R.string.pol));
                如果(settings.getInt(GRUPA_JEZYKOWA,1)== 2){
                    lekcja7.setText(的getString(R.string.ros));
                    lekcja8.setText(的getString(R.string.ros));
                }其他{
                    lekcja7.setText(的getString(R.string.brak));
                    lekcja8.setText(的getString(R.string.brak));
                }
                lekcja9.setText(的getString(R.string.brak));
                打破;
            }
            案例3:{//地区希罗达
                lekcja1.setText(的getString(R.string.his));
                lekcja2.setText(的getString(R.string.wf));
                lekcja3.setText(的getString(R.string.mat));
                lekcja4.setText(的getString(R.string.rel));
                lekcja5.setText(的getString(R.string.ang));
                如果(settings.getInt(GRUPA_JEZYKOWA,1)== 1){
                    lekcja6.setText(的getString(R.string.niem));
                    lekcja7.setText(的getString(R.string.niem));
                }其他{
                    lekcja6.setText(的getString(R.string.brak));
                    lekcja7.setText(的getString(R.string.brak));
                }
                lekcja8.setText(的getString(R.string.brak));
                lekcja9.setText(的getString(R.string.brak));
                打破;
            }
            情况4:{// Czwartek
                lekcja1.setText(的getString(R.string.mat));
                lekcja2.setText(的getString(R.string.fiz));
                lekcja3.setText(的getString(R.string.wok));
                lekcja4.setText(的getString(R.string.geo));
                lekcja5.setText(的getString(R.string.ang));
                lekcja6.setText(的getString(R.string.chem));
                如果(settings.getInt(GRUPA_INFORMATYKA,1)== 2){
                    lekcja7.setText(的getString(R.string.inf));
                }否则如果(((settings.getInt(GRUPA_INFORMATYKA,1)== 2))及&放大器;!((settings.getInt(GRUPA_JEZYKOWA,1)= 3))){
                    lekcja7.setText(的getString(R.string.brak pre));
                }其他{
                    lekcja7.setText(的getString(R.string.brak));
                }
                如果(settings.getInt(GRUPA_JEZYKOWA,1)== 3){
                    lekcja8.setText(的getString(R.string.por));
                    lekcja9.setText(的getString(R.string.por));
                }
                打破;
            }
            壳体5:{//Piątek
                lekcja1.setText(的getString(R.string.mat));
                lekcja2.setText(的getString(R.string.mat));
                lekcja3.setText(的getString(R.string.bio));
                lekcja4.setText(的getString(R.string.pol));
                lekcja5.setText(的getString(R.string.pol));
                lekcja6.setText(的getString(R.string.edb));
                如果(settings.getBoolean(WDZ,FALSE)){
                    lekcja7.setText(的getString(R.string.wdz));
                }其他{
                    lekcja7.setText(的getString(R.string.brak));
                }
                lekcja8.setText(的getString(R.string.brak));
                lekcja9.setText(的getString(R.string.brak));
                打破;
            }
        }        返回rootView;
    }
}

P.S。我使用的Andr​​oid Studio IDE中。


解决方案

  preferenceManager.getDefaultShared preferences(本);

要获得共享preferences 通过这种方法,你必须通过上下文对象。所以,你可以通过你的活动在这种情况下,因为片段的方法来获取父活动活动上下文扩展分类。 code是这样的:

  preferenceManager.getDefaultShared preferences(getActivity());

祝你好运!

I'm developing small application for my class mates, which is adjustable timetable. It uses Fragments to display each day of week as a nice, finger-swipable UI:

In SettingsActivity.class there are preferences (defined in xml), which automatically store settings in SharedPreferences. The problem is that class, in which Fragment is defined, is static. I cannot use there non-static method reference, like that one:

SharedPreferences settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);

Through reading documentation on developer.android.com and googling for help, I discovered that to use SharedPreferences created by SettingsActivity.class, I have to use PreferenceManager, like that:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

Now it doesn't show me the error about non-static reference, but I don't know how to refer SettingsActivity.class in getDefaultSharedPreferences argument, because I'm in Fragment static class, so I cannot use "this".

I have also tried to create SharedPreference object outside of the static class. However, all uses of that object complain that "non-static field cannot be referenced from a static context".

It's important for me to use SharedPreferences there, because later I'm going to implement lessons (TextViews) color changing according to an hour, and that is also toggle-able in Settings.

Here is the code of that fragment class:

public static class Dzien extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public Dzien() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView lekcja1 = (TextView) rootView.findViewById(R.id.lekcja1);
        TextView lekcja2 = (TextView) rootView.findViewById(R.id.lekcja2);
        TextView lekcja3 = (TextView) rootView.findViewById(R.id.lekcja3);
        TextView lekcja4 = (TextView) rootView.findViewById(R.id.lekcja4);
        TextView lekcja5 = (TextView) rootView.findViewById(R.id.lekcja5);
        TextView lekcja6 = (TextView) rootView.findViewById(R.id.lekcja6);
        TextView lekcja7 = (TextView) rootView.findViewById(R.id.lekcja7);
        TextView lekcja8 = (TextView) rootView.findViewById(R.id.lekcja8);
        TextView lekcja9 = (TextView) rootView.findViewById(R.id.lekcja9);


        //Here is where I try to create SharedPreference object
        SharedPreferences settings =  PreferenceManager.getDefaultSharedPreferences(this);


        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1: { // Poniedziałek
                lekcja1.setText(getString(R.string.wos));
                lekcja2.setText(getString(R.string.mat));
                lekcja3.setText(getString(R.string.ang));
                lekcja4.setText(getString(R.string.gw));
                lekcja5.setText(getString(R.string.his));
                lekcja6.setText(getString(R.string.wf));
                lekcja7.setText(getString(R.string.pp));
                if (settings.getInt(GRUPA_INFORMATYKA, 1) == 1) {
                    lekcja8.setText(getString(R.string.inf));
                } else {
                    lekcja8.setText(getString(R.string.brak));
                }
                lekcja9.setText(getString(R.string.brak));
                break;
            }
            case 2: { // Wtorek
                lekcja1.setText(getString(R.string.mat));
                lekcja2.setText(getString(R.string.pp));
                lekcja3.setText(getString(R.string.rel));
                lekcja4.setText(getString(R.string.wf));
                lekcja5.setText(getString(R.string.pol));
                lekcja6.setText(getString(R.string.pol));
                if (settings.getInt(GRUPA_JEZYKOWA, 1) == 2) {
                    lekcja7.setText(getString(R.string.ros));
                    lekcja8.setText(getString(R.string.ros));
                } else {
                    lekcja7.setText(getString(R.string.brak));
                    lekcja8.setText(getString(R.string.brak));
                }
                lekcja9.setText(getString(R.string.brak));
                break;
            }
            case 3: { // Sroda
                lekcja1.setText(getString(R.string.his));
                lekcja2.setText(getString(R.string.wf));
                lekcja3.setText(getString(R.string.mat));
                lekcja4.setText(getString(R.string.rel));
                lekcja5.setText(getString(R.string.ang));
                if (settings.getInt(GRUPA_JEZYKOWA, 1) == 1) {
                    lekcja6.setText(getString(R.string.niem));
                    lekcja7.setText(getString(R.string.niem));
                } else {
                    lekcja6.setText(getString(R.string.brak));
                    lekcja7.setText(getString(R.string.brak));
                }
                lekcja8.setText(getString(R.string.brak));
                lekcja9.setText(getString(R.string.brak));
                break;
            }
            case 4: { // Czwartek
                lekcja1.setText(getString(R.string.mat));
                lekcja2.setText(getString(R.string.fiz));
                lekcja3.setText(getString(R.string.wok));
                lekcja4.setText(getString(R.string.geo));
                lekcja5.setText(getString(R.string.ang));
                lekcja6.setText(getString(R.string.chem));
                if (settings.getInt(GRUPA_INFORMATYKA, 1) == 2) {
                    lekcja7.setText(getString(R.string.inf));
                } else if (((settings.getInt(GRUPA_INFORMATYKA, 1) == 2)) && ((settings.getInt(GRUPA_JEZYKOWA, 1) != 3))) {
                    lekcja7.setText(getString(R.string.brakpre));
                } else {
                    lekcja7.setText(getString(R.string.brak));
                }
                if (settings.getInt(GRUPA_JEZYKOWA, 1) == 3) {
                    lekcja8.setText(getString(R.string.por));
                    lekcja9.setText(getString(R.string.por));
                }
                break;
            }
            case 5: { // Piątek
                lekcja1.setText(getString(R.string.mat));
                lekcja2.setText(getString(R.string.mat));
                lekcja3.setText(getString(R.string.bio));
                lekcja4.setText(getString(R.string.pol));
                lekcja5.setText(getString(R.string.pol));
                lekcja6.setText(getString(R.string.edb));
                if (settings.getBoolean(WDZ, false)) {
                    lekcja7.setText(getString(R.string.wdz));
                } else {
                    lekcja7.setText(getString(R.string.brak));
                }
                lekcja8.setText(getString(R.string.brak));
                lekcja9.setText(getString(R.string.brak));
                break;
            }


        }

        return rootView;
    }
}

P.S. I'm using Android Studio IDE.

解决方案

PreferenceManager.getDefaultSharedPreferences(this);

To get SharedPreferences by this method you must pass Context object. So you can pass your Activity in this case, because Fragment has method to get parent Activity and Activity is extended class from Context. Code looks like this :

PreferenceManager.getDefaultSharedPreferences(getActivity());

Good luck!

这篇关于创建共享preferences内片段对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆