gpt4 book ai didi

android - 使用应用程序更新 ListView 小部件

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:06 25 4
gpt4 key购买 nike

当我尝试从我的应用程序更新内容时,我的 App Widget 出现问题。我的小部件包含一个 ListView 。

ListView 工作正常,每 30 分钟更新一次,但我在应用程序中进行更改时也需要更新它。这是我的代码:

public class WidgetListProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
ComponentName component;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);

component=new ComponentName(context,WidgetListProvider.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listViewWidget);
appWidgetManager.updateAppWidget(component, remoteViews);
}

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

private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_list_layout);

Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(R.id.listViewWidget, svcIntent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
return remoteViews;
}

}

最佳答案

将此代码放入您要更新内容的应用程序中。

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetIds[] = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetListProvider.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listViewWidget);

关于android - 使用应用程序更新 ListView 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28941472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com