Listivew Widget not updating automatically(Listivew 小部件不会自动更新)
问题描述
我的朋友已经完成了一个应用程序,现在他为他的应用程序创建了一个小部件,可用于以列表视图的方式显示任务列表.首先,当他拖动小部件并安装在主屏幕上时,它会正常工作并以列表视图的方式显示任务.
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.
他的问题是,当他在应用程序中进行更改然后返回主屏幕时,没有反映任何更改.他正在为此使用 onReceive()
方法来更新小部件
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()
--->通知所有指定的AppWidget
实例中的指定collection view 使其当前数据无效.
notifyAppWidgetViewDataChanged()
--->Notifies the specified collection view in all the specified AppWidget
instances to invalidate their currently data.
因此,当您调用 notifyAppWidgetViewDataChanged()
时,将调用 RemoteViewsFactory
的 onDataSetChanged()
方法,该方法用作您的适配器列表视图
.您可以在 onDataSetChanged()
中进行 web 服务/网络相关操作、从数据库中获取数据操作和其他操作,获取响应并将其添加到您的数据集中,可能是 ArrayList
或任何此类 Collection
.
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
.
您可以参考Using App Widgets with Collections
这篇关于Listivew 小部件不会自动更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Listivew 小部件不会自动更新
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01