单击按钮刷新后如何刷新小部件列表视图? [英] How to refresh widget listview when button refresh is clicked?

查看:103
本文介绍了单击按钮刷新后如何刷新小部件列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建带有ListViewWidget.可以在ListView中显示Web Service的结果.我创建了一个类,该类是我的WidgetService并扩展到RemoteViewsService,在RemoteViewsFactory上,我称我的ListViewProvider具有Web Service.我添加了一个刷新按钮,单击该按钮将再次调用WidgetService来创建列表视图.

I'm creating a Widget with ListView on it. Displaying the result from the Web Service in the ListView is okay. I created a class which is my WidgetService that extends to RemoteViewsService and on RemoteViewsFactory I call my ListViewProvider that has the Web Service. I added a refresh button that when clicked it will call the WidgetService again to created the list view.

这是我的WidgetProvider

public class WidgetProvider extends AppWidgetProvider {

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

private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);

//ListView
    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(appWidgetId, R.id.listViewWidget, svcIntent);
    remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);

    //REFRESH
    PendingIntent updatePendingIntent = PendingIntent.getService(context, 0, svcIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.ivRefreshWidget, updatePendingIntent);

 return remoteViews;
}

正如您在刷新部分看到的那样,我尝试使用PendingIntent调用WidgetService,但是在测试时不会刷新列表. 预先感谢您的帮助.

As you can see in the refresh part I tried using PendingIntent to call the WidgetService but when testing it doesn't refresh the list. Thank you for the help in advance.

推荐答案

在我的onUpdate中,我设置了要在onReceive中调用的Action,并且还将appWidgetIds也要用于onReceive

In my onUpdateI set an Action to be called in my onReceive, and also put the appWidgetIds to be used as well in the onReceive

Intent refreshIntent = new Intent(context, WidgetProvider.class);
        refreshIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

然后在onReceive中,我称onUpdate

if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            if (appWidgetIds != null && appWidgetIds.length > 0) {
                Toast.makeText(context, "REFRESH", Toast.LENGTH_SHORT).show();
                this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
            }
        }
    }

这篇关于单击按钮刷新后如何刷新小部件列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆