【发布时间】:2018-07-23 17:21:54
【问题描述】:
我有一个应该显示信息列表的小部件。但是,在研究了大量教程和文档后,我无法弄清楚为什么它没有显示列表。它总是显示空视图。 WidgetService.java 类甚至从未被调用过。
我的代码可以在github上找到here:
RecipeWidgetProvider.java
public class RecipeWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
long recipeId = preferences.getLong(WidgetConfigurationActivity.PREF_PREFIX_KEY+appWidgetId, 0);
Intent serviceIntent = new Intent(context, WidgetService.class);
serviceIntent.putExtra(WidgetService.EXTRA_RECIPE_ID, recipeId);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
Intent intent = new Intent(context, RecipeListActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget_recipes);
widget.setRemoteAdapter(appWidgetId, R.id.widget_configuration_list_view, serviceIntent);
widget.setEmptyView(R.id.widget_recipe_list_view, R.id.empty_view);
widget.setOnClickPendingIntent(R.id.widget_container, pendingIntent);
//appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_recipe_list_view);
appWidgetManager.updateAppWidget(appWidgetId, widget);
}
}
WidgetService.java
public class WidgetService extends RemoteViewsService {
public static String EXTRA_RECIPE_ID = "recipeId";
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
int recipeId = intent.getIntExtra(EXTRA_RECIPE_ID, -1);
return new WidgetViewFactory(getApplicationContext(), recipeId);
}
}
WidgetViewFactory.java
public class WidgetViewFactory implements RemoteViewsService.RemoteViewsFactory {
private Context context;
private long recipeId;
private ArrayList<IngredientEntity> ingredients = new ArrayList<>();
public WidgetViewFactory(Context context, long recipeId) {
this.context = context;
this.recipeId = recipeId;
RecipeLocalSource source = RecipeLocalSource.getInstance(context);
RecipeEntity recipe = source.getRecipe(recipeId);
if (recipe != null) {
ingredients.addAll(recipe.getIngredients());
}
}
@Override
public void onCreate() {
Log.e("","");
}
@Override
public void onDataSetChanged() {
RecipeLocalSource source = RecipeLocalSource.getInstance(context);
RecipeEntity recipe = source.getRecipe(recipeId);
if (recipe != null) {
ingredients.addAll(recipe.getIngredients());
}
}
@Override
public void onDestroy() {
Log.e("","");
}
@Override
public int getCount() {
Log.d("","");
return ingredients.size();
}
@Override
public RemoteViews getViewAt(int position) {
final IngredientEntity ingredient = ingredients.get(position);
RemoteViews row = new RemoteViews(context.getPackageName(),
R.layout.item_widget_ingredient);
row.setTextViewText(R.id.item_widget_ingredient_name, ingredient.getIngredient());
row.setTextViewText(R.id.item_widget_ingredient_quantity_measure, String.format("%s %s", ingredient.getQuantity(), ingredient.getMeasure()));
return row;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return true;
}
}
【问题讨论】:
-
您的帖子中能否提供相关代码?
-
@DanSchneider 我做了