android - 小部件中的ListView不更新

标签 android android-listview android-widget

我想从服务更新小部件中的 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/

相关文章:

android - 如何在我的 ListView 上启用 LongCLicks?

android - 如何在 android 中获取多个公共(public) facebook 页面提要数据?

javascript - android: webview 不使用自定义 WebViewClient 加载 javascript

android - 如何检测 ListView 项是否足以触发滚动

android - 在 ListView 的第一个项目上添加图标 : scrolling it, 图标随机移动

android - 如何从 ListView 中获取字符串?

java - 如何将A类中创建的变量的值传递到B类中

javascript - 从 SQlite 中提取 Lat Long 并显示在 webview 上

android - 避免在 WindowManager 中更新的自定义 View 中的布局更改动画

java - 编程和实现自定义拨号器 UI