0

我有一个包含列表视图和按钮的 appwidget

listview 提供来自远程 mysql 数据库 mysql 的数据并显示它

但是我想让我们使用它的按钮直接从 appwidget 刷新列表视图,而不是等待 appwidgetinfo 中定义的时间

这是我的 widgetprovider 类:

public class WidgetProvider extends AppWidgetProvider {

// String to be sent on Broadcast as soon as Data is Fetched
// should be included on WidgetProvider manifest intent action
// to be recognized by this WidgetProvider to receive broadcast
public static final String DATA_FETCHED = "com.wordpress.laaptu.DATA_FETCHED";

/*
 * this method is called every 30 mins as specified on widgetinfo.xml this
 * method is also called on every phone reboot from this method nothing is
 * updated right now but instead RetmoteFetchService class is called this
 * service will fetch data,and send broadcast to WidgetProvider this
 * broadcast will be received by WidgetProvider onReceive which in turn
 * updates the widget
 */
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        Intent serviceIntent = new Intent(context, RemoteFetchService.class);
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetIds[i]);
        context.startService(serviceIntent);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

    // which layout to show on widget
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);

    // RemoteViews Service needed to provide adapter for ListView
    Intent svcIntent = new Intent(context, WidgetService.class);
    // passing app widget id to that RemoteViews Service
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    // setting a unique Uri to the intent
    // don't know its purpose to me right now
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
    // setting adapter to listview of the widget
    remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
            svcIntent);
    // setting an empty view in case of no data
    remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
    return remoteViews;
}

/*
 * It receives the broadcast as per the action set on intent filters on
 * Manifest.xml once data is fetched from RemotePostService,it sends
 * broadcast and WidgetProvider notifies to change the data the data change
 * right now happens on ListProvider as it takes RemoteFetchService
 * listItemList as data
 */
@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (intent.getAction().equals(DATA_FETCHED)) {
        int appWidgetId = intent.getIntExtra(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(context);
        RemoteViews remoteViews = updateWidgetListView(context, appWidgetId);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }

}

}

我在这里使用了本教程的代码源

我在布局中添加了带有 refresh_button id 的按钮,但我不知道当用户按下它时如何启动列表视图的更新

我该如何解决这个问题

4

1 回答 1

0
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget);

onDataSetChanged()在 AppWidgetProvider.onUpdate 方法中添加上面的代码,这将在您可以刷新列表视图的帮助下调用您的方法

我有同样的问题,但终于解决了

于 2013-11-22T09:01:08.480 回答