小部件中的ListView不会更新

时间:2013-12-20 12:57:39

标签: 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永远不会被调用。未显示日志消息,并且在主屏幕上没有任何反应。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

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

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中测试过它。