0
我已經看到這個問題的相關問題,但我無法弄清楚爲什麼這不適合我。Android部件列表查看處理點擊
我期待在Widget Provider類中的onReceive()方法被調用,因爲列表項被點擊,但沒有任何反應。
一些評論被包括在內,它們也顯示了我一直在嘗試的內容。
相關代碼:
主要的應用程序佈局的ListView定義是這樣的:
<ListView
android:id="@+id/events_list"
style="@android:style/TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
行被定義與RelativeLayout的兩個嵌套的線性佈局,不知道這是分享重要。
getViewAt()通過查看工廠實現,並且這裏的功能:
@Override
public RemoteViews getViewAt(int position) {
if (events_ack == null) {
Log.e("LIST", "getViewAt (null) events_ack");
return null;
}
RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.row);
row.setTextViewText(R.id.event_name, events_ack[position].name);
row.setTextViewText(R.id.description, events_ack[position].description);
row.setTextViewText(R.id.publicTV, (events_ack[position].is_public == 0) ? "Public: False" : "Public: True");
row.setTextViewText(R.id.timeTV, events_ack[position].time);
row.setViewVisibility(R.id.price, View.GONE);
// download and set image BONUS QUESTION- does not work through AsyncTask using future insted (images are downloaded but not shown).
row.setImageViewBitmap(R.id.image, pic);
Bundle extras = new Bundle();
extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
// Make it possible to distinguish the individual on-click
// action of a given item
row.setOnClickFillInIntent(R.id.events_list, fillInIntent);
//extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id);
//i.putExtras(extras);
//i.setAction(CasusWidgetProvider.ITEM_CLICK);
//row.setOnClickFillInIntent(R.layout.casus_widget, i); //event_name?
// ??
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
appWidgetManager.updateAppWidget(appWidgetId, rv);
//appWidgetManager.updateAppWidget(appWidgetId, row);
return row;
}
控件提供者類具有的onUpdate()方法:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.w("CASUS", "UPDATE function called");
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget);
rv.setRemoteAdapter(appWidgetIds[i], R.id.events_list, intent);
Intent clickIntent=new Intent(context, WidgetService.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
clickIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
clickIntent.setAction(ITEM_CLICK);
PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.events_list, clickPI);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.events_list); //?
clickIntent = new Intent(context, CasusWidgetProvider.class);
clickIntent.setAction(UPDATE_LIST);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntentRefresh = PendingIntent.getBroadcast(context, 0, clickIntent, 0);
rv.setOnClickPendingIntent(R.id.update, pendingIntentRefresh);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
fetchData(context, appWidgetIds);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}