android - Listview Widget 不会自动更新

标签 android widget auto-update

我的 friend 已经完成了一个应用程序,现在他为他的应用程序创建了一个小部件,可以用来以 ListView 的方式显示任务列表。首先,当他拖动小部件并将其安装到主屏幕时,它将正常工作并以 ListView 方式显示任务。

他的问题是,当他在他的应用程序中进行更改然后返回到他的主屏幕时,没有任何更改反射(reflect)出来。他正在使用 onReceive() 方法来更新这个小部件

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

    }
}

最佳答案

我建议你试试下面粘贴的代码:

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

notifyAppWidgetViewDataChanged() --->通知所有指定的AppWidget实例中的指定 Collection View 使其当前数据失效。

因此,当您调用 notifyAppWidgetViewDataChanged() 时,RemoteViewsFactoryonDataSetChanged() 方法将被调用,它充当您的适配器 ListView 。您可以在 onDataSetChanged() 中执行与网络服务/网络相关的操作、从数据库中获取数据的操作以及其他操作,获取响应并将其添加到您的数据集中,可能是 ArrayList 或任何此类 Collection

可以引用Using App Widgets with Collections

关于android - Listview Widget 不会自动更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18524974/

相关文章:

android - 从 Android 应用程序发送电子邮件

flutter - 未调用 dispose 方法

auto-update - Android -- 从网络服务器自动安装第三方 apk 文件

java - 从自定义适配器中的 listView 中删除对象

java - 改造 - Json 响应可以根据类型包含不同的字段

android - 在android上运行模拟器时出错

html - 如何在 GWT 中的 HTML 小部件内的链接上捕获点击事件?

javascript - 关于如何处理 javascript 小部件的想法?

c - Windows 上 C/GTK 应用程序的简单更新机制

android - 严重的 Android Studio 更新问题