1

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 ?

Swayam
  • 16,294
  • 14
  • 64
  • 102

2 Answers2

1

no, you can not. Since the

static {
}

blocks are called when you refers the first time the class. So, I think, before the onCreate si called. In order to accesss SharedPreference you need a context, and the context is valid after the onCreate . So you can not access SharedPreference in the static block

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I edited my question. Please check and suggest anything if possible. – Swayam Jun 20 '12 at 08:06
  • where are you trying to retrive the sharedPreference? – Blackbelt Jun 20 '12 at 08:10
  • I want to do it inside the static block. But, it gives error. The code that is given in my question is working, but i want to include the SharedPreferences to it too. – Swayam Jun 20 '12 at 08:11
  • you can not. 1. because the Context instance you need does not exists 2. you are calling getSharedPreferences(), but of course is, this.getSharedPreferences() (reference to the current activity instace), and since this can not be static you can not access to the sharedpreferences – Blackbelt Jun 20 '12 at 08:17
  • Then how do I create an array whose size is stored in the phone memory ? – Swayam Jun 20 '12 at 08:18
  • why you can not create in the onCreate()? Why do you need a static block? – Blackbelt Jun 20 '12 at 08:21
  • it shows error in getGroupView() at getGroup(groupPosition).toString() – Swayam Jun 20 '12 at 08:23
  • because the array is static? Why need you the array to be declared as static? – Blackbelt Jun 20 '12 at 08:26
  • I need the array to be static to declare it's contents inside the static block. – Swayam Jun 20 '12 at 08:28
  • Are you suggesting that I should remove all static references and do everything inside the onCreate() ?? – Swayam Jun 20 '12 at 08:28
  • depends on your purpose. If you need it static, do it static. But do you need really it static? – Blackbelt Jun 20 '12 at 08:34
  • Would the ExpandableView getGroupView(..) work if the parent[] is not declared as soon as the class is loaded ? – Swayam Jun 20 '12 at 08:41
  • getGroupView(..) is a method of ExpandableListAdapter. Am I right? Then in order to make it works you have to submit the data set (you array) to the adapter and set the instance of ExpandableListAdapter to the ExpandableListView. So definitly yes it works. IMHO you need to review some key concept in OOP and android itself. – Blackbelt Jun 20 '12 at 08:49
  • Okay, will do it. Hope it works. Otherwise, I am gonna bother you once again I guess. – Swayam Jun 20 '12 at 08:54
  • you should open a new Qustion, since it is not more relate with the SharedPreference issue. – Blackbelt Jun 20 '12 at 08:57
0

First, I have to say that you approach sounds a bit peculiar. Why would you want to declare the array as static and init it before the onCreate? If this is truly a requirement, then I am not sure there is a solution as onCreate() is the first time you get access to the context.

I could propose that you define your array as static but then initialize it in onCreate (before you use it). Unless that array is used outside your activity, this should work.

Perhaps if you shared more details (like why it has to be static and initiated before onCreate) you could receive more help.

Nearchos
  • 153
  • 1
  • 11