Listivew窗口小部件不会自动更新 [英] Listivew Widget not updating automatically

查看:124
本文介绍了Listivew窗口小部件不会自动更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的朋友已经完成了一个应用程序,现在他创建了一个小工具,他的应用程序,可以用来显示任务列表视图方式列表。在第一次时,他拖动窗口小部件并安装它会被罚款工作的主屏幕,以及显示列表视图方式的任务。

他的问题是,当他做了变化,他的应用程序,然后回到他的主屏幕,没有变化反映出来。他是用的onReceive()方法,这样一来更新窗口小部件

 进口android.app.PendingIntent;
进口android.appwidget.AppWidgetManager;
进口android.appwidget.AppWidgetProvider;
进口android.content.ComponentName;
进口android.content.Context;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.content.Shared preferences.Editor;
进口android.net.Uri;
进口android.util.Log;
进口android.widget.RemoteViews;

公共类WidgetTaskSchedular扩展AppWidgetProvider {
    私人最终字符串变量=CalendarViewSample:
            + this.getClass()的getName();

    共享preferences共享preFER;

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        super.onReceive(背景下,意图);
        如果(intent.getAction()。等于(update_widget))
        {
            INT []的appid =新INT [1];
            共享preFER = context.getShared preferences(TaskWidget,0);
            的appid [0] =共享prefer.getInt(widget_key,0);
            Log.i(TAG,Appwidgetid+的appid [0]);
            的OnUpdate(背景下,AppWidgetManager.getInstance(上下文)的appid);

        }
    }
    公共静态字符串EXTRA_WORD =
            com.capsone.testing.calendar.WORD;
    @燮pressWarnings(德precation)
    @覆盖
    公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
            INT [] appWidgetIds){
        super.onUpdate(背景下,appWidgetManager,appWidgetIds);
         的for(int i = 0; I< appWidgetIds.length;我++){

             共享preFER = context.getShared preferences(TaskWidget,0);
             共享preferences.Editor编辑=共享prefer.edit();
             editor.putInt(widget_key,appWidgetIds [I]);
             editor.commit();
             诠释一个=共享prefer.getInt(widget_key,0);
             Log.i(TAG,共享preferencewidgetid:+ A);

              // ID = appWidgetIds [I]
              Log.i(TAG,LengthofWidget:+ appWidgetIds.length);
             // Log.i(TAG,TestAppWidget:+ ID);
              意图int​​entWidgetService =新的意图(背景下,WidgetService.class);
              intentWidgetService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds [I]);
              intentWidgetService.setData(Uri.parse(intentWidgetService.toUri(Intent.URI_INTENT_SCHEME)));

              RemoteViews远程视窗=新RemoteViews(context.getPackageName()
                                                  R.layout.widgetlayout);

              remoteView.setRemoteAdapter(appWidgetIds [I],R.id.listWidget,
                                      intentWidgetService);

              意图clickIntent =新的意图(背景下,ActionBarActivity.class);
              PendingIntent clickPendingIntent = PendingIntent
                                      .getActivity(上下文,0,
                                                    clickIntent,
                                                    PendingIntent.FLAG_UPDATE_CURRENT);
              remoteView.setPendingIntentTemplate(R.id.listWidget,clickPendingIntent);
             组件名组件=新的组件名(背景下,WidgetTaskSchedular.class);
              appWidgetManager.updateAppWidget(组件,远程视窗)
            }

    }
}
 

解决方案

我建议你尝试code粘贴下面:

 组件名组件=新的组件名(背景下,WidgetTaskSchedular.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,R.id.listWidget);
appWidgetManager.updateAppWidget(组件,远程视窗)
 

notifyAppWidgetViewDataChanged() --->通知已在所有指定的 AppWidget 实例指定集合视图无效的目前的数据。

所以,当你调用 notifyAppWidgetViewDataChanged()那么的 onDataSetChanged()方法 RemoteViewsFactory 将被称为是作为一个适配器为你的的ListView 。你可以做的网络服务/网络相关操作,获取数据从数据库和其他业务操作 onDataSetChanged(),得到了响应,并将其添加到您的数据集这可能的ArrayList 或任何这样的集合

您可以参考使用App小部件集合

My friend have completed one application and now he created one widget for his application which can be used to display the list of task in list view manner. In first when he drag the widget and install in home screen it will be working fine as well as display the task in list view manner.

His problem is, when he made changes in his application and then come back to his home screen, No changes reflected. He is using onReceive() method for this one to update the widget

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

public class WidgetTaskSchedular extends AppWidgetProvider {
    private final String TAG = "CalendarViewSample:"
            + this.getClass().getName();

    SharedPreferences sharedprefer;

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if(intent.getAction().equals("update_widget"))
        {
            int[] appid=new int[1];
            sharedprefer=context.getSharedPreferences("TaskWidget", 0);
            appid[0]=sharedprefer.getInt("widget_key",0);
            Log.i(TAG,"Appwidgetid"+appid[0]);
            onUpdate(context, AppWidgetManager.getInstance(context),appid);

        }
    }
    public static String EXTRA_WORD=
            "com.capsone.testing.calendar.WORD";
    @SuppressWarnings("deprecation")
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
         for (int i=0; i<appWidgetIds.length; i++) {

             sharedprefer=context.getSharedPreferences("TaskWidget", 0);
             SharedPreferences.Editor editor=sharedprefer.edit();
             editor.putInt("widget_key", appWidgetIds[i]);
             editor.commit();
             int a= sharedprefer.getInt("widget_key", 0);
             Log.i(TAG,"Sharedpreferencewidgetid:"+a);

              //ID=appWidgetIds[i];
              Log.i(TAG,"LengthofWidget:"+appWidgetIds.length);
             // Log.i(TAG,"TestAppWidget:"+ID);
              Intent intentWidgetService=new Intent(context, WidgetService.class);
              intentWidgetService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
              intentWidgetService.setData(Uri.parse(intentWidgetService.toUri(Intent.URI_INTENT_SCHEME)));

              RemoteViews remoteView=new RemoteViews(context.getPackageName(),
                                                  R.layout.widgetlayout);

              remoteView.setRemoteAdapter(appWidgetIds[i], R.id.listWidget,
                                      intentWidgetService);

              Intent clickIntent=new Intent(context, ActionBarActivity.class);
              PendingIntent clickPendingIntent=PendingIntent
                                      .getActivity(context, 0,
                                                    clickIntent,
                                                    PendingIntent.FLAG_UPDATE_CURRENT);
              remoteView.setPendingIntentTemplate(R.id.listWidget, clickPendingIntent);
             ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
              appWidgetManager.updateAppWidget(component, remoteView);
            }

    }
}

解决方案

I suggest you to try the code pasted below:

ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listWidget);
appWidgetManager.updateAppWidget(component, remoteView);

notifyAppWidgetViewDataChanged() --->Notifies the specified collection view in all the specified AppWidget instances to invalidate their currently data.

So when you call notifyAppWidgetViewDataChanged() then the onDataSetChanged() method of RemoteViewsFactory will be called which is serving as a Adapter for your ListView. You can do web-service/network related operations, data fetching operation from database and other operations in onDataSetChanged(), get the response and add it to your data-set which maybe ArrayList or any such Collection.

You can refer Using App Widgets with Collections

这篇关于Listivew窗口小部件不会自动更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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