-2

i'm working on an android widget that fetches content from my api.

What i'm using now is a custom function that uses a callback included in onUpdate => api.getLastItems(MyCallback).

MyWidget provider :

public void onUpdate(Context _context, AppWidgetManager _appWidgetManager,int[] _appWidgetIds) {

  api.getLastItems(new requestCallback() {

     @Override
     public void onFinish(Object object) {
        ArrayList<String> announcs_titles   = (ArrayList<String>)object;
        final int N = widgetId.length;
        for (int i = 0; i < N; i++) {
           Intent myIntent = new Intent(context, WidgetService.class);
           myIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetId[i]);
           myIntent.putStringArrayListExtra("itemsTitles",items_titles);
           ...
           appWidgetManager.updateAppWidget(widgetId[i], widget);
        }
     }

  });

  super.onUpdate(context, appWidgetManager, widgetId);
}

Because RemoteViewsService.RemoteViewsFactory use a special function getViewAt(int position) that needs a position to begin working, I used this kind of structure to let my widget wait until my ArrayList of Items gets filled.

is that right or there is another method for that? and how could i refresh my widget by a clickEvent (i mean the way to do it, not the PendingIntent.getActivity code)

OWZY
  • 531
  • 7
  • 15

1 Answers1

0

I fixed the problem by using a custom function : load() that i call inside WidgetProvider onReceive()

@Override
public void onReceive(final Context context, Intent intent) {
// Check if nothing is loading and call function load()

}
private void load(){
... 
//1- fetch content from api and set result to a global variable
//2- Call onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
//add condition to check is loading is completed and my global variable contains something.
}

if someone needs some help about this method, contact me

OWZY
  • 531
  • 7
  • 15