在使用自定义行更新我的xamarin小部件列表视图时,我需要一些帮助。 我找到了一些例子,但直到现在才行。 这是我的代码。
[BroadcastReceiver(Label = "@string/widget_name")]
[IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE" })]
[MetaData("android.appwidget.provider", Resource = "@xml/widget_word")]
public class WordWidget : AppWidgetProvider
{
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
int N = appWidgetIds.Length;
for (int i = 0; i < N; ++i)
{
RemoteViews remoteViews = updateWidgetListView(context, appWidgetIds[i]);
appWidgetManager.UpdateAppWidget(appWidgetIds[i], remoteViews);
}
base.OnUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId)
{
RemoteViews remoteViews = new RemoteViews(context.PackageName, Resource.Layout.widget_word);
Intent svcIntent = new Intent(context, typeof(WidgetService));
svcIntent.PutExtra(AppWidgetManager.ExtraAppwidgetId, appWidgetId);
svcIntent.SetData(Android.Net.Uri.Parse(svcIntent.ToUri(IntentUriType.Scheme)));
remoteViews.SetRemoteAdapter(appWidgetId, Resource.Id.listViewWidget, svcIntent);
remoteViews.SetEmptyView(Resource.Id.listViewWidget, Resource.Id.empty_view);
return remoteViews;
}
}
base.OnUpdate(context,appWidgetManager,appWidgetIds); - &GT;在这一行我的应用程序小部件崩溃
我的UpdateWidget服务:
[Service(Permission = "android.permission.BIND_REMOTEVIEWS", Exported = false)]
public class WidgetService : RemoteViewsService
{
public override IRemoteViewsFactory OnGetViewFactory(Intent intent)
{
int appWidgetId = intent.GetIntExtra(AppWidgetManager.ExtraAppwidgetId,
AppWidgetManager.InvalidAppwidgetId);
return (new ListWidgetProvider(this.ApplicationContext, intent));
}
}
public class ListWidgetProvider : RemoteViewsService.IRemoteViewsFactory
{
private Intent intent;
List<MarketDataParams> DataList = new List<MarketDataParams>();
private Context context = null;
private int appWidgetId;
public ListWidgetProvider(Context context, Intent intent)
{
this.context = context;
this.intent = intent;
appWidgetId = intent.GetIntExtra(AppWidgetManager.ExtraAppwidgetId, AppWidgetManager.InvalidAppwidgetId);
}
public bool HasStableIds
{
get
{
throw new NotImplementedException();
}
}
public RemoteViews LoadingView
{
get
{
throw new NotImplementedException();
}
}
public int ViewTypeCount
{
get
{
throw new NotImplementedException();
}
}
public IntPtr Handle
{
get
{
throw new NotImplementedException();
}
}
public int Count
{
get
{
return DataList.Count();
}
}
private void populateListItem()
{
}
public long GetItemId(int position)
{
return position;
}
public RemoteViews GetViewAt(int position)
{
WidgetData wd = new WidgetData();
string w_data = wd.GetSecurities();
string f_data = wd.GetUserFivoritList();
ArrayList listItemList = new ArrayList();
MarketData secData = JsonConvert.DeserializeObject<MarketData>(w_data);
MarketData fivData = JsonConvert.DeserializeObject<MarketData>(f_data);
var updateViews = new RemoteViews(context.PackageName, Resource.Layout.widget_word);
if (secData.data.Count > 0)
{
foreach (var item in secData.data)
{
MarketDataParams DataParams = new MarketDataParams();
DataParams.Name = item.Name;
DataParams.Fields.LastDealRate = item.Fields.LastDealRate;
DataParams.Fields.DailyNISRevenue = item.Fields.DailyNISRevenue;
DataParams.Fields.BaseRateChangePercentage = item.Fields.BaseRateChangePercentage;
DataParams.Id = item.Id;
DataList.Add(DataParams);
}
}
updateViews.SetTextViewText(Resource.Id.blog_title,DataList[0].Name);
updateViews.SetTextViewText(Resource.Id.creator, DataList[0].Fields.BaseRate);
return updateViews;
}
public void OnCreate()
{
throw new NotImplementedException();
}
public void OnDataSetChanged()
{
throw new NotImplementedException();
}
public void OnDestroy()
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
}