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.