In my application, i need to declare an Array based on the value stored by the user in the SharedPreference variable. The problem is that the array needs to be declared in a static block as the size of the array needs to be declared before the onCreate() is called in my class.
I have an ExpandableList in my Activity with parent array as the dates of the next seven days.
static int plannerSlotCount=7;
static public String[] parent = new String[plannerSlotCount];
static
{
Calendar cal = Calendar.getInstance();
String strdate = null;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
int i;
for(i=0;i<plannerSlotCount;i++)
{
if (cal != null) {
strdate = sdf.format(cal.getTime());
}
parent[i] = strdate;
cal.add(Calendar.HOUR_OF_DAY,24);
}
}
If i don't declare the array inside the static block, then I get an error at
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
So,I have to declare the content of the array in the static block itself I guess.
The thing is that I want to change the number of days to display(it is currently set to 7). So, I thought of saving the number in a SharedPreference variable and accessing it to initialize the array.
The problem I am facing, is that
SharedPreferences preferences = getSharedPreferences(Settings.PREF_SETTINGS_FILE_NAME, MODE_PRIVATE);
final int slotCounter = preferences.getInt("slotCount", 7);
gives me an Error saying that
Cannot make a static reference to the non-static method getSharedPreferences(String, int) from the type ContextWrapper
Is there any possible way to achieve this ?