当按下按钮android时,在appwidget内启动listview的更新

时间:2013-10-07 15:35:13

标签: android events listview button android-appwidget

我有一个appwidget,其中包含listview和buton

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

但按钮我想让它直接从appwidget刷新listview而不要等待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);
    }

}

}

我使用了本教程的代码源here

我在布局中添加了refresh_button id的按钮 但我不知道如何在用户按下时启动listview的更新

如何解决此问题

1 个答案:

答案 0 :(得分:0)

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

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

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