StackView に基づいており、いくつかのアイテムを表示する必要があるアプリケーションのウィジェットに取り組んでいます。アイテムの数は、ユーザーの操作によって異なる場合があります。たとえば、お気に入りメニューのようなものだとしましょう。StackView のビューを生成するために、RemoteViewsFactory の子孫を実装しました。
public class StackRemoteViewsFactory implements RemoteViewsFactory {
private List<Item> data;
private Context context;
public StackRemoteViewsFactory(Context context) {
this.context = context;
data = new DAO(context).getFavouriteCards();
}
@Override
public void onDataSetChanged() {
data = new DAO(context).getWidgetItems(); // refresh widget dataset
}
@Override
public int getCount() {
return data.size();
}
@Override
public RemoteViews getViewAt(int position) {
Item item = data.get(position);
// this is my problem and will be explained below -
// here I'm getting IndexOutOfBoundsException, position = 0
...
}
// I have omitted other methods to avoid verbosity
}
すべてが最初のウィジェットの開始時にうまく機能します - ウィジェットは空のビューを正常に表示し、すべてうまくいきます。新しい要素をお気に入りに追加した後、すぐに AppWidgetManager に通知します。
AppWidgetManager widgetManager = AppWidgetManager.getInstance(getActivity());
ComponentName component = new ComponentName(getActivity(), MyWidgetProvider.class);
int[] widgetIds = widgetManager.getAppWidgetIds(component);
widgetManager.notifyAppWidgetViewDataChanged(widgetIds, R.id.widgets_stack);
この時点ですべて順調です。データセットが正常に変更され、お気に入りのアイテムがウィジェットに表示されます。しかし、お気に入りから単一の要素を削除し、お気に入りが空になると (もちろん AppWidgetManager に通知されます)、getViewAtMethod で IndexOutOfBoundsException を受け取ります。
10-15 17:26:36.810: E/AndroidRuntime(30964): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
まず、火災getCount()
後に何が返されるかを確認しました-それは0でした。したがって、0を返すときにonDataSetChanged()
なぜRemoteViewsFactory
呼び出すのかわかりません。理解する限り、通常のアダプターのようなものなので、この動作は完全に混乱します。だから多分それについての私の提案は間違っていて、私は何か間違ったことをしていますか? getViewAt()
getCount()
RemoteViewFactory
更新今日、いくつかの GUI バグを修正しようとしている間ではなく
、ウィジェットのレイアウトを使用するように変更しました。そして、今のところすべてうまく機能しています。さらなる実験では、StackView と同じ問題があることが示されています。私が理解している限り、子孫を扱ういくつかの側面があります-誰かが私の仮定を確認/反論できますか? ListView
StackView
AdapterViewFlipper
AdapterViewAnimator