0

I am creating a widget that will show some datas for the user from a database. One of the datas depends on a parameter that can be set in my settings activity. I save this parameter with sharedpreferences so I can use it anywhere in my code. In an activity I could use getApplicationContext, but here where I tell the widget what to do, it doesnt work. What should I use instead of getApplicationContext?

UPDATED

public class plWidget extends AppWidgetProvider{

    SharedPreferences sharedPreferences;
     String loadedWeightType;

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
        Toast.makeText(context, "deleted", 2500).show();
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);

         String Wcal="0",Wfat="0",Wprot="0",Wcarb="0",Wsport="0";

         final int N = appWidgetIds.length;

         for (int i = 0;i<N;i++)
        {
            int awID = appWidgetIds[i];
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);

            GlobalVars.setSulyType(loadedWeightType);           
        Log.i("SULYYYY", GlobalVars.getSulyType());

        long now = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date resultdate = new Date(now);
        Log.i("ASAS", sdf.format(resultdate));


        hornot database = new hornot(context);
        database.open();



         int ccc = database.checkDataExists(sdf.format(resultdate), sdf.format(resultdate));
        if (ccc==0){
            Log.i("nulla", "0");
            Log.i("nulla", GlobalVars.getSulyType());

            Wcal="0";
            Wfat="0";
            Wprot="0";
            Wcarb="0";
        }

        else{

        database.getDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
         Wcal = GlobalVars.getSums();

        database.FATgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
         Wfat = GlobalVars.getSums();

        database.PROTEINgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
         Wprot = GlobalVars.getSums();

        database.CARBSgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
         Wcarb = GlobalVars.getSums();

                }

        int ddd = database.checkDataExistsSports(sdf.format(resultdate), sdf.format(resultdate));
        if (ddd==0){
            Wsport="0";

        }
        else{

            if (loadedWeightType.equals("kilogramm"))
                    {
            database.SportgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
            // Wsport = GlobalVars.getSums();
             Wsport= "kilogramm";


                    }
            else if (loadedWeightType.equals("pound"))
                    {
            database.SportgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
            Wsport="pound";
                    }

        }

        RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
        v.setTextViewText(R.id.tvwidgetUpdate, Wcal+Wfat+Wprot+Wcarb+Wsport);
        appWidgetManager.updateAppWidget(awID, v);
        database.close();



        }


    }

    public void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { 
        SharedPreferences prefs = context.getSharedPreferences(plWidget.class + Integer.toString(appWidgetId),
                Context.MODE_WORLD_READABLE);
         loadedWeightType= prefs.getString("weighttype", "kilogramm");
    }

}

Thanks in advance!

UPDATE

As usual I do the load funciton:

 public void LoadWeightType(){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        loadedWeightType= sharedPreferences.getString("weighttype", "kilogramm");
      }

With this in a normal activity, I can load the weighttype. I guess that updateAppWidget function should somehow substitue this function.

Jani Bela
  • 1,660
  • 4
  • 27
  • 50
  • 1
    Please see http://stackoverflow.com/questions/2748268/get-preferences-in-appwidget-provider – Jack May 07 '12 at 22:26
  • getSharedPreferences does not work in my case: Cannot make a static reference to the non-static method getSharedPreferences() from the type PreferenceManager – Jani Bela May 07 '12 at 23:03

1 Answers1

1
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(MyConfigActivity.NAME + Integer.toString(appWidgetId), Context.MODE_WORLD_READABLE);

I call updateAppWidget from onUpdate for each instance of my widget, passing in the parameters. The passed in Context has getSharedPreferences defined, and I specify which set of preferences to get based on the name of the configuration activity and the id of the widget. Here's the sdk reference for the getSharedPreferences function: http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29

dwemthy
  • 471
  • 3
  • 9
  • Wow. I got a pop up message around the fifth comment I left suggesting taking this to chat and to avoid long comment conversations, but the Android chat was set up so you could only chat with approval. If someone deleted them I would have hoped they'd leave a comment about it. But anyway, beyond making sure your widget and activity are part of the same application and making sure that you are storing your preferences with the same key you use to retrieve them I'm not sure what else to suggest. – dwemthy May 09 '12 at 19:53