问题描述
我用Google搜索,发现code。使用操作列表视图数据的 RemoteViewsService 和 RemoteViewsService.RemoteViewsFactory。如下
I googled and found the code to manipulate listview data using RemoteViewsService and RemoteViewsService.RemoteViewsFactory. as below
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包含
WidgetService.java contains
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);
}
}
和写的所有codeS取出来自web服务数据的的onCreate WidgetViewsFactory实现RemoteViewsService.RemoteViewsFactory
and wrote all the codes for fetching data from webservice in onCreate of WidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory
有关手动或自动更新记录每一个5秒,我通过服务方法找到更新如下所述
For updating records manually or automatically every 5seconds, I found Update via Service methods as below mentioned
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;
}
}
}
如何自动更新列表视图。我知道该怎么上述更新的TextView。
How to update the listview automatically.. I know how to update textview as mentioned above.
推荐答案
您可以更新使用的ListView notifyAppWidgetViewDataChanged()方法AppWidgetManager.您将获得AppWidgetManager的情况下,得到了AppWidgetIds和呼叫 appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,的ViewID);
You can update the ListView by using notifyAppWidgetViewDataChanged() method of AppWidgetManager. You will have to get the instance of AppWidgetManager, get the AppWidgetIds and the call appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, viewId);
伪code,
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetIds[] = appWidgetManager.getAppWidgetIds(
new ComponentName(context, WidgetProvider.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listview);
当你调用 notifyAppWidgetViewDataChanged()
那么的 onDataSetChanged()
方法 RemoteViewsFactory
将被称为是作为一个适配器为您的ListView。你可以做web服务的东西,其他操作 onDataSetChanged()
,得到了响应,并将其添加到您的数据集这可能ArrayList的或任何这样的集合。
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 stuff and other operations in onDataSetChanged()
, get the response and add it to your data-set which maybe ArrayList or any such Collection.
有关进一步读数/引用您可以查看保持信息的收集新鲜部分从文档。你也可以检查一个演示的例子,从我的 github上。
For further readings/reference you can check Keeping Collection Data Fresh part from the docs. Also you can check a demo example from my github.
这篇关于Android的 - 小部件可滚动列表视图手动刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!