2

I am making a widget that has a grid view at the top, and a listview under it, upon user selection from the grid, the list view below gets updated. but I am facing these problems: first, the widget doesn't show the gridView until I re-size it twice. second, when the grid appears, the list view gets updated but at the second click. then the value doesn't change no matter how many times I click after it.

I have tried forcing the update on the view,but nothing seems to work.

Here is my update widget function:

public static int viewIndex = 0;
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

    Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
    int Height = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
    RemoteViews views;

    if (Height<300) {
        views = new RemoteViews(context.getPackageName(), R.layout.ingredient_widget_provider);
        Intent intent = new Intent(context,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.appwidget_text,pendingIntent);
    }
    else{
        views = getIngredientsList(context,appWidgetId);

    }
   appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,R.id.widget_list_view);
    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);

}

and here is the function that sets the view:

private static RemoteViews getIngredientsList(Context context,int appWidgetId) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_ings_layout);
    Intent intent = new Intent(context,GridWidgetService.class);
    views.setRemoteAdapter(R.id.widget_grid_view,intent);

        if (viewIndex != 0) {
            appWidgetManager.updateAppWidget(appWidgetId, views);
            Intent ListIntent = new Intent(context, ListWidgetService.class);
            ListIntent.putExtra("extra_id", viewIndex);
            views.setRemoteAdapter(R.id.widget_list_view, ListIntent);
        }
    Intent appIntent = new Intent(context, IngredientWidgetProvider.class);
    appIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    appIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent appPendingIntent = PendingIntent.getBroadcast(context, 0,
            appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    views.setPendingIntentTemplate(R.id.widget_grid_view, appPendingIntent);
    return views;

}

here is the onRecieve:

 public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)){
         viewIndex = intent.getIntExtra("extra_id_grid",0);
        int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        Toast.makeText(context,"position "+viewIndex,Toast.LENGTH_LONG).show();

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

        updateAppWidget(context,appWidgetManager,appWidgetId);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,R.id.widget_list_view);
    }
    super.onReceive(context, intent);
}

the update:

 @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    update(context,appWidgetManager,appWidgetIds);
}
public static void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}

the onOptionsChanged

@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
    updateAppWidget(context,appWidgetManager,appWidgetId);
    super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}

Update: my github link: https://github.com/basma11elmihy/bakingApp

1 Answers1

0

Eventually I was able to solve this issue: turned out I can't make a network request in the remote view adapter which I was doing,that's why it only updates after second re-size, now I made the network request in an intent service, from this service I forced the update, Like this:

 public static void InflateList(Context context, int viewIndex) {
    Intent intent = new Intent(context,GridIntentService.class);
    con = context;
    intent.setAction("InflateList");
    intent.putExtra("viewIndex",viewIndex);
    context.startService(intent);
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    if(intent != null) {
        if (intent.getAction().equals("InflateGrid")) {
            getGridData();
        }
        if (intent.getAction().equals("InflateList")){
            int index = intent.getIntExtra("viewIndex",0);
            getListData(index);
        }
    }
}

private void getListData(final int index) {
    StringRequest request = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            GsonBuilder gsonBuilder = new GsonBuilder();
            Gson gson = gsonBuilder.create();
            RecipesModel[] recipesModels = gson.fromJson(response, RecipesModel[].class);
            mData = new ArrayList<>(Arrays.asList(recipesModels));
            model = mData.get(index);
            ings = model.getIngredients();
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(con);
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(con, IngredientWidgetProvider.class));
        //    appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_list_view);
            IngredientWidgetProvider.update(con,appWidgetManager,appWidgetIds,mData,ings);


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
           Log.e("error","volley list error");

        }
    });
    RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(request);
}

then as the code above I sent the data in the parameters in the update function in the widgetProvider main class, from there I sent the data to RemoteViewFactory using an intent. but turns out the widget intent is different from the activity intent, it doesn't send Parcalable data, so I transformed the parcalable data to a string, and sent it as extra. using this way:

RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_ings_layout);
    Intent intent = new Intent(context,GridWidgetService.class);
    String dumb = new Gson().toJson(mData);
    intent.setData(Uri.fromParts("scheme",dumb,null));
    views.setRemoteAdapter(R.id.widget_grid_view,intent);

and got the data back in the remote adapter like this:

 public ListRemoteViewFactory(Context context, Intent intent) {
    this.context = context;
    this.stringData = String.valueOf(intent.getData().getSchemeSpecificPart());

}

because the widget intent also has to use setData not putExtra Like the activity Intent that will make it call OnGetViewFactory everytime not only one time. Its explained here:

RemoteViewFactory onDataSetChanged() only called once per notifyAppWidgetViewDataChanged()