Listview小部件不会自动更新

3

我的朋友完成了一个应用程序,并创建了一个小部件,可以用于以列表视图方式显示任务列表。当他第一次将小部件拖动并安装到主屏幕上时,它能够正常工作,同时以列表视图方式显示任务。

但是他遇到了问题,当他在应用程序中进行更改后返回主屏幕时,没有反映出任何更改。他使用 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);
            }

    }
}
2个回答

9
我建议您尝试下面粘贴的代码:
ComponentName component=new ComponentName(context,WidgetTaskSchedular.class);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listWidget);
appWidgetManager.updateAppWidget(component, remoteView);

notifyAppWidgetViewDataChanged() ---> 通知所有指定的 AppWidget 实例中的特定集合视图使它们无效。

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

您可以参考“使用带集合的小部件”


0

您尝试过这里提到的解决方案吗:

如何动态更新小部件(而不是等待30分钟调用onUpdate)?

请注意,上述仅触发更新,小部件本身需要获取所需数据。

请注意,虽然广播接收器可以在小部件中注册,但在更新后,它们会被垃圾回收,因此不是可靠的解决方案。

最后一个选择是注册AlarmManager以触发执行小部件更新的服务。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接