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