gpt4 book ai didi

android - 小部件中的 ListView 不更新

转载 作者:行者123 更新时间:2023-11-29 00:24:31 26 4
gpt4 key购买 nike

我想通过服务更新小部件中的 ListView。为此,我的服务中有这段代码:

Intent intent = new Intent(ctx, ListProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

remoteViews.setRemoteAdapter(widgetId, R.id.listViewWidget, intent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);

我确定服务正在运行并且代码已执行,但在主屏幕上的小部件中没有任何反应。我的 ListProvider 看起来像这样:

public class ListProvider implements RemoteViewsFactory
{
public ListProvider(Context context, Intent intent)
{
Log.d("ME", "new ListProvider");

this.context = context;
appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

populateListItem();
}

private void populateListItem()
{
for (int i = 0; i < 10; i++)
{
ListItem listItem = new ListItem();
listItem.heading = "Heading" + i;
listItem.content = i + " This is the content";
listItemList.add(listItem);
}
}

@Override
public int getCount()
{
return listItemList.size();
}

@Override
public long getItemId(int position)
{
return position;
}

@Override
public RemoteViews getViewAt(int position)
{
final RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.list_row);
ListItem listItem = listItemList.get(position);

remoteView.setTextViewText(R.id.heading, listItem.heading);
remoteView.setTextViewText(R.id.content, listItem.content);

return remoteView;
}

@Override
public RemoteViews getLoadingView()
{
return null;
}

@Override
public int getViewTypeCount()
{
return 1;
}

@Override
public boolean hasStableIds()
{
return true;
}

// Some other override methods

public class ListItem
{
public String heading,content;
}
}

这是我的 AppWidgetProvider:

public class MyWidgetProvider extends AppWidgetProvider
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}

但我认为 ListProvider 从未被调用过。日志消息不显示,主屏幕上没有任何反应。有什么想法吗?

最佳答案

使用服务更新远程适配器:

public class UpdateWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new ListProvider(this.getApplicationContext(), intent);
}
}

并直接使用服务类而不是 RemoteViewFactory:

for (int i = 0; i < appWidgetIds.length; ++i) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

Intent serviceIntent = new Intent(context, UpdateWidgetService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetIds[i], R.id.stackWidgetView, serviceIntent);
remoteViews.setEmptyView(R.id.stackWidgetView, R.id.no_data_imageview);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}

从未在 listView 中测试过它。

关于android - 小部件中的 ListView 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704139/

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