里面appwidget列表视图的推出更新时,按钮pressed机器人 [英] launch update of listview inside appwidget when button is pressed android

查看:225
本文介绍了里面appwidget列表视图的推出更新时,按钮pressed机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个ListView和appwidget一个布顿

在ListView从远程MySQL数据库MySQL允许数据并显示它

但按钮我想我们来说,从appwidget直接刷新ListView和不等待在appwidgetinfo规定的时间

这是我的widgetprovider类:

 公共类WidgetProvider扩展AppWidgetProvider {//字符串对广播尽快发送的数据被取出
//应包含在WidgetProvider清单的意图行动
//由该WidgetProvider识别接收广播
公共静态最后弦乐DATA_FETCHED =com.word press.laaptu.DATA_FETCHED/ *
 *在widgetinfo.xml这是指定此方法称为每30分钟一班
 *方法也被称为在每次手机重启从此方法是什么
 *更新,现在反而RetmoteFetchService类被称为本
 *服务将获取数据,并传送广播WidgetProvider这
 *广播将WidgetProvider的onReceive接收反过来
 *更新微件
 * /
@覆盖
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
        INT [] appWidgetIds){
    最终诠释N = appWidgetIds.length;
    的for(int i = 0; I< N;我++){
        意图serviceIntent =新意图(背景下,RemoteFetchService.class);
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetIds [I]);
        context.startService(serviceIntent);
    }
    super.onUpdate(背景下,appWidgetManager,appWidgetIds);
}私人RemoteViews updateWidgetListView(上下文的背景下,诠释appWidgetId){    //哪个布局上显示小部件
    RemoteViews remoteViews =新的RemoteViews(context.getPackageName()
            R.layout.widget_layout);    // RemoteViews服务需要提供为ListView的适配器
    意图svcIntent =新意图(背景下,WidgetService.class);
    //通过应用程序的小工具ID到RemoteViews服务
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
    //设置一个唯一的URI的意图
    //现在不知道它的目的,我
    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);
    返回remoteViews;
}/ *
 *它接收广播按动作集的意图过滤器上
 *一次的Manifest.xml数据来自RemotePostService牵强,它发送
 *广播和WidgetProvider通知来改变数据的数据变化
 *现在发生在ListProvider,因为它需要RemoteFetchService
 * listItemList数据
 * /
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    super.onReceive(背景下,意图);
    如果(intent.getAction()。等于(DATA_FETCHED)){
        INT appWidgetId = intent.getIntExtra(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(上下文);
        RemoteViews remoteViews = updateWidgetListView(背景下,appWidgetId);
        appWidgetManager.updateAppWidget(appWidgetId,remoteViews);
    }}

}

我用这个教程的这里

我与refresh_button ID的布局添加的按钮
但我不知道如何启动列表视图的更新时,用户preSS它

我怎么能解决这个问题。


解决方案

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

中添加AppWidgetProvider.onUpdate方法,在code这会给调用你的 onDataSetChanged()法中的帮助,您可以刷新列表视图

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

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

解决方案

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

这篇关于里面appwidget列表视图的推出更新时,按钮pressed机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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