【问题标题】:Recyclerview arraylist throws an outofbounds exception in androidRecyclerview arraylist在android中抛出一个outofbounds异常
【发布时间】:2017-07-24 17:39:27
【问题描述】:

我的回收器视图存在问题,或者更准确地说,是用于在每个视图中设置值的辅助数组列表。

我有一个发送到我的适配器的项目列表,并在其构造函数中创建了一个与该列表大小相同的数组列表。项目的数组列表正常工作,但包含所有整数的第二个数组列表表示越界。快速日志显示情况并非如此,那里有一个 1 的列表,但每次我尝试访问它时,它在索引 0 处的出界。

类定义

RecyclerView recyclerView;
ItemAdapter itemAdapter;
List<Item> items;
List<Boolean> itemsSelected;
List<Integer> quantities;

创建时

  quantities = new ArrayList<>();

为适配器创建项目列表并创建等长 1 列表的方法

 for (int i = 0; i <categories.size() ; i++) {
               long length =  dataSnapshot.child(categories.get(i)).getChildrenCount();
              Iterator<DataSnapshot> toAdd= dataSnapshot.child(categories.get(i)).getChildren().iterator(); // this returns all the items in each category
                for (int j = 0; j <length ; j++) {
                    DataSnapshot newData = toAdd.next();
                    items.add(newData.getValue(Item.class));
                    //quantities.add(Integer.valueOf(1));
                    //itemsSelected.add(Boolean.valueOf(false));
                    Log.d(TAG, "onDataChange: debug: "+ newData.toString());

                }


            }
            for (int i = 0; i < items.size() ; i++) {
                quantities.add(i,Integer.valueOf(1));
                itemsSelected.add(i,Boolean.valueOf(false));
            }
            itemAdapter = new ItemAdapter(items);
            itemAdapter.notifyDataSetChanged();

在上面的代码中,注释掉的两行是另一种创建列表的尝试,但它也有同样的错误,所以我把它移到了其他地方。

我在适配器构造函数中也是这样做的

 public ItemAdapter(List<Item> items){
        this.items = items;
        for (int i = 0; i < items.size() ; i++) {
            quantities.add(i,Integer.valueOf(1));
            itemsSelected.add(i,Boolean.valueOf(false));
            Log.d(TAG, "ItemAdapter: quan "+ quantities.get(i));
            Log.d(TAG, "ItemAdapter: bool "+ itemsSelected.get(i));
        }

Logd 显示位置 典型的日志输出

ItemAdapter: quan 1
bindItem: quan [1, 1, 1, 1, 1, 1, 1, 1]

我做错了什么。即使在绑定项目时该列表也存在,因为如果我记录整个列表而不是单个索引,则会输出整个列表,但 get(index) 会导致越界。

感谢您抽出宝贵时间查看此内容。

编辑:适配器代码

 private class ItemAdapter extends RecyclerView.Adapter<ItemHolder>{
    List<Item> items;
    Item item;

    public ItemAdapter(List<Item> items){
        this.items = items;
        for (int i = 0; i < items.size() ; i++) {
            quantities.add(i,Integer.valueOf(1));
            itemsSelected.add(i,Boolean.valueOf(false));
            Log.d(TAG, "ItemAdapter: quan "+ quantities.get(i));
            Log.d(TAG, "ItemAdapter: bool "+ itemsSelected.get(i));
        }
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view= layoutInflater.inflate(R.layout.singleitem,parent,false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, int position) {
        item = items.get(position);
        holder.bindItem(item,position);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }


}

【问题讨论】:

  • 你能贴出你的适配器代码吗
  • 刚刚为你添加了它

标签: android arraylist android-recyclerview indexoutofboundsexception


【解决方案1】:

您如何访问适配器中的quantities 列表?

解决这个问题的方法是将数量列表传递给适配器的构造函数,然后在向两个列表添加值之后调用notifyDataSetChanged() 方法(记住添加和不重置列表)。这将确保两个列表都使用适配器进行更新。

只是一个小小的疑问。为什么要在构造函数中设置 for 循环。你打算实现什么目标?

【讨论】:

  • 您好,感谢您的回答。 for 循环在那里是因为我一直在尝试找出问题所在。应该将数量设置为与项目列表完全相同的长度。我通过在片段类中声明它来访问适配器中的数量。我将尝试您首先创建两个列表然后将它们传递给适配器并将结果返回给您的解决方案,尽管我认为我之前尝试过。这是因为当我在视图中突出显示一个项目时,所有与该视图对应的项目都被突出显示,所以我试图用这些列表来跟踪它
  • 其他视图被点亮。那么这与您的列表无关。检查this堆栈帖子!
  • 您好,感谢您的帮助。我通过在同一位置创建列表并将它们传递进来,将列表放入适配器中。至于其他视图,我检查了链接,但最终以这种方式继续。列表的目的是跟踪已选择的内容,以便当我向上和向下滚动时选择仍然存在。不确定 onViewRecycled 是否会持续
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 2012-01-31
  • 2012-06-03
  • 2012-09-30
  • 2019-03-09
相关资源
最近更新 更多