Android TV在BrowseFragment中重新加载数据

时间:2016-10-26 15:21:27

标签: android indexoutofboundsexception android-tv browsefragment

我正在编写Android TV应用程序并遇到了这个问题。我必须在接收事件时用新的行替换BrowseFragment中的所有行。 我尝试了什么:

mRowsAdapter.clear();
...
mRowsAdapter.add(add new rows here);

其中mRowsAdapter是带有ListRow项的ArrayObjectAdapter。 也试过这种方法:

mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
setAdapter(mRowsAdapter);
mRowsAdapter.add(add new rows here);

来自this question的解决方案。它们都不起作用。我一直在接受IOBE。这是日志:

                               java.lang.IndexOutOfBoundsException: Index: 7, Size: 0
                                   at java.util.ArrayList.get(ArrayList.java:411)
                                   at android.support.v17.leanback.widget.ArrayObjectAdapter.get(ArrayObjectAdapter.java:56)
                                   at android.support.v17.leanback.app.ListRowDataAdapter.get(ListRowDataAdapter.java:65)
                                   at android.support.v17.leanback.widget.ItemBridgeAdapter.getItemViewType(ItemBridgeAdapter.java:248)
                                   at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5046)
                                   at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5172)
                                   at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5153)
                                   at android.support.v17.leanback.widget.GridLayoutManager.getViewForPosition(GridLayoutManager.java:971)
                                   at android.support.v17.leanback.widget.GridLayoutManager$2.createItem(GridLayoutManager.java:1464)
                                   at android.support.v17.leanback.widget.SingleRow.appendVisibleItems(SingleRow.java:111)
                                   at android.support.v17.leanback.widget.Grid.appendVisibleItems(Grid.java:371)
                                   at android.support.v17.leanback.widget.GridLayoutManager.appendVisibleItems(GridLayoutManager.java:1702)
                                   at android.support.v17.leanback.widget.GridLayoutManager.fastRelayout(GridLayoutManager.java:1764)
                                   at android.support.v17.leanback.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:1888)
                                   at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3374)
                                   at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3183)
                                   at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3627)
                                   at android.view.View.layout(View.java:17519)
                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                   at android.support.v17.leanback.widget.ScaleFrameLayout.onLayout(ScaleFrameLayout.java:172)
                                   at android.view.View.layout(View.java:17519)
                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                   at android.view.View.layout(View.java:17519)
                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                   at android.view.View.layout(View.java:17519)
                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                   at android.view.View.layout(View.java:17519)
                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                   at android.view.View.layout(View.java:17519)
                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                   at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                   at android.view.View.layout(View.java:17519)
                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                   at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
                                   at android.view.View.layout(View.java:17519)
                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                                   at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                                   at android.view.Choreographer.doFrame(Choreographer.java:619)
com.cloudify E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                                   at android.os.Handler.handleCallback(Handler.java:751)
                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                   at android.os.Looper.loop(Looper.java:154)
                                   at android.app.ActivityThread.main(ActivityThread.java:6077)
                                   at java.lang.reflect.Method.invoke(Native Method)
                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

请帮忙。谢谢。

3 个答案:

答案 0 :(得分:0)

您的索引超出范围,因为适配器中的内部对象数组正在尝试使用自己的大小来确定添加项目的位置。

当你调用.add()时,它会在内部执行此操作 -

/**
 * Adds an item to the end of the list.
 *
 * @param item The item to add to the end of the list.
 */
public void add(Object item) {
    add(mItems.size(), item);
}
/**
 * Inserts an item into this list at the specified index.
 *
 * @param index The index at which the item should be inserted.
 * @param item The item to insert into the list.
 */
public void add(int index, Object item) {
    mItems.add(index, item);
    notifyItemRangeInserted(index, 1);
}

这意味着你调用clear的一些方法暂时不会出现问题而且内部索引与实际情况有关,并且arraylist选择了IOBE。也许尝试使用add(0,yourArray)代替。

修改

尝试在添加对象之前添加某种验证。类似的东西:

if((myAdapter.size() - 1) > indexImTryingToAddAt)
{
    myAdapter.addAll(indexImTryingToAddAt, arrayOfObjects);
}
else
{
    Log.d("index is larger than current adapter", indexImTryingToAddAt+"");
}

答案 1 :(得分:0)

尝试这样做:

    mRowsAdapter.clear();
    mRowsAdapter.add(add new rows here);
    mRowsAdapter.notifyArrayItemRangeChanged(0, mRowsAdapter.size());

答案 2 :(得分:0)

我解决类似问题的方式:

如果Array为空,则不能将具有set索引的对象放入ArrayObjectAdapter。

  

https://developer.android.com/reference/android/support/v17/leanback/widget/ArrayObjectAdapter

     

void add(int index,Object item)

     

在指定索引处将项插入此适配器。 如果索引是> size()将抛出异常。

我不知道我的解决方案有多顺利,但您可以执行以下操作:

使用“虚拟”对象填充数组

mRowsAdapter.add(0, new ListRow(new HeaderItem(""), new ArrayObjectAdapter()));
mRowsAdapter.add(1, new ListRow(new HeaderItem(""), new ArrayObjectAdapter()));
mRowsAdapter.add(2, new ListRow(new HeaderItem(""), new ArrayObjectAdapter()));

当回调完成后,替换它们:

mRowsAdapter.replace(0, new ListRow(new HeaderItem("List1"), list1));
mRowsAdapter.replace(1, new ListRow(new HeaderItem("List2"), list2));
mRowsAdapter.replace(2, new ListRow(new HeaderItem("List3"), list3));

这样你也可以将一个项目计数器添加到HeaderItem中:

mRowsAdapter.replace(2, new ListRow(new HeaderItem("List3 (" + list3.size() + ")"), list3));