RemoteViewsFactory在空數據集時調用getViewAt

[英]RemoteViewsFactory called getViewAt when empty dataset


I'm working on widget for my application which based on StackView and should display some items. Count of items can vary due to user actions - lets say it is some kind of favorites menu. I have implemented RemoteViewsFactory descendant to generate views for StackView:

我正在為我的應用程序開發基於StackView的小部件,並且應該顯示一些項目。由於用戶操作的不同,項目的數量也會有所不同。我實現了RemoteViewsFactory子類來為StackView生成視圖:

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
}

All works well at first widget start - widget successfully displays empty view and all goes well. After I added some new element to favourites I notify AppWidgetManager immediately:

在第一個小部件啟動時,所有的工作都很好——widget成功地顯示了空視圖,並且一切順利。在我向favorites添加了一些新元素之后,我立即通知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);  

All is well at this point - dataset successfully changed and I see my favorite item on widget. But when I remove my single element from favorites and favorites become empty (AppWidgetManager notified of course) I receive IndexOutOfBoundsException in my getViewAtMethod:

在這一點上一切都很好——數據集成功地改變了,我在小部件上看到了我最喜歡的項目。但是當我從favorites和favorites中移除單個元素時,favorites變為空時(AppWidgetManager當然會通知),我就會在getViewAtMethod中收到IndexOutOfBoundsException:

10-15 17:26:36.810: E/AndroidRuntime(30964): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0  

First of all I've checked what getCount() returns after onDataSetChanged() fires - it was 0. So I don't understand why RemoteViewsFactory calls getViewAt() when getCount() returns 0. As far as I understanding, RemoteViewFactory is like a usual adapter so this behavior is completely confusing me. So maybe my suggestions about it are incorrect and I doing something wrong?

首先,我檢查了onDataSetChanged()觸發后getCount()返回的值,它是0。所以我不理解為什么當getCount()返回0時RemoteViewsFactory調用getViewAt()。就我所知,RemoteViewFactory就像一個通常的適配器,所以這種行為完全讓我感到困惑。也許我的建議是錯誤的,而我做錯了什么?

UPDATE
Today I've changed my widget layout to use ListView instead of StackView while trying to fix some gui bug. And all works well at now. Further experiment shows that AdapterViewFlipper have same issue as StackView. As far as I understood there are some aspects working with AdapterViewAnimator descendants - can anyone confirm/refute my assumption?

更新今天我更改了我的小部件布局,使用ListView而不是StackView來修復一些gui bug。現在一切都很好。進一步的實驗表明,AdapterViewFlipper與StackView有相同的問題。據我所知,有一些方面與AdapterViewAnimator的后代有關——有人能證實/駁斥我的假設嗎?

1 个解决方案

#1


10  

Same problem here. Since the solution is not yet coming (Android 4.4 here and it still happens), I think the best solution for now is basically a workaround to avoid the crash. Some devs will call it "hack"...

同樣的問題在這里。由於解決方案還沒有出現(Android 4.4已經出現了),我認為目前最好的解決方案基本上是一個解決方案,以避免崩潰。一些開發人員會稱之為“黑客”……

For now:

現在:

public RemoteViews getViewAt(int position) {

    if (position >= getCount())
            return getLoadingView();
    }
    //.... Rest of your normal configuration
}

Make sure you return a view properly on getLoadingView()

確保在getLoadingView()上返回正確的視圖


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/10/15/72573159b392f682f1e5e9ff0c4f6f14.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com