launch update of listview inside appwidget when button is pressed android

血红的双手。 提交于 2019-12-12 05:29:51

问题


I have an appwidget which contains a listview and a buton

the listview gives data from remote mysql database mysql and it display it

but the button I want to us it to refresh listview from appwidget directly and don't wait for time defined in appwidgetinfo

here is my widgetprovider class :

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);
    }

}

}

I used the code-source of this tutorial here

I added the button in the layout with refresh_button id but I don't know how to launch update of listview when user press it

how can I fix this issue


回答1:


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

add the above code in AppWidgetProvider.onUpdate method this will give call to your onDataSetChanged() method with the help of that you can refresh the listview

I had the same problem but finally solved it



来源:https://stackoverflow.com/questions/19228861/launch-update-of-listview-inside-appwidget-when-button-is-pressed-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!