Android – 具有可滚动列表视图的小部件手动刷新

我googled并发现使用RemoteViewsService和RemoteViewsService.RemoteViewsFactory操作列表视图数据的代码.如下

Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds[i]);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews widget = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
widget.setRemoteAdapter(appWidgetIds[i], R.id.lstAppointments,svcIntent);

WidgetService.java包含

public class WidgetService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return (new WidgetViewsFactory(this.getApplicationContext(), intent));
    }

    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return super.onBind(intent);
    }
}

并写了所有的代码,从webservice中获取数据,在OnCreate of WidgetViewsFactory中实现了RemoteViewsService.RemoteViewsFactory

为了每5秒钟手动或自动更新记录,我发现通过服务方式更新如下所述

public class WordWidget extends AppWidgetProvider {

    static int value = 1;
    Handler handler = new Handler();
    MyRunnable myRunnable;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // To prevent any ANR timeouts, we perform the update in a service
        myRunnable = new MyRunnable(context);
        handler.postDelayed(myRunnable, 1000);
    }

    class MyRunnable implements Runnable {

        Context context;

        public MyRunnable(Context context) {
            this.context = context;
        }

        public void run() {
            context.startService(new Intent(context, UpdateService.class));
            handler.postDelayed(myRunnable, 1000);
        }
    }

    public static class UpdateService extends Service {
        @Override
        public void onStart(Intent intent, int startId) {
            // Build the widget update for today
            RemoteViews updateViews = buildUpdate(this);

            // Push update for this widget to the home screen
            ComponentName thisWidget = new ComponentName(this, WordWidget.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(this);
            manager.updateAppWidget(thisWidget, updateViews);
        }

        public RemoteViews buildUpdate(Context context) {
            RemoteViews updateViews;

            // Build an update that holds the updated widget contents
            updateViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget_word);

            Log.e("value", String.valueOf(value));
            updateViews.setTextViewText(R.id.word_title, "Title");
            updateViews.setTextViewText(R.id.word_type, String.valueOf(value));
            updateViews.setTextViewText(R.id.definition, String.valueOf(value));

            value += 1;

            // When user clicks on widget it opens www.google.com
            Intent defineIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://www.google.co.in/"));
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    defineIntent, 0);
            updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent);

            return updateViews;
        }

        @Override
        public IBinder onBind(Intent intent) {
            // We don't need to bind to this service
            return null;
        }
    }
}

如何自动更新listview ..我知道如何更新如上所述的textview.

最佳答案
您可以使用AppWidgetManagernotifyAppWidgetViewDataChanged()方法更新ListView.您将必须获取AppWidgetManager的实例,获取AppWidgetIds和调用appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,viewId);

伪代码,

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetIds[] = appWidgetManager.getAppWidgetIds(
                           new ComponentName(context, WidgetProvider.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview);

当调用notifyAppWidgetViewDataChanged()时,将调用RemoteViewsFactory的onDataSetChanged()方法作为ListView的适配器.您可以在onDataSetChanged()中执行Web服务和其他操作,获取响应并将其添加到您的数据集中,这也可能是ArrayList或任何此类集合.

如需进一步阅读/参考,您可以从文档中查看Keeping Collection Data Fresh部分.另外,您可以从我的github中查看演示示例.

转载注明原文:Android – 具有可滚动列表视图的小部件手动刷新 - 代码日志