E / UncaughtException:java.lang.ArrayIndexOutOfBoundsException:length = 100;指数= -1

时间:2018-07-17 19:28:40

标签: java android firebase android-recyclerview

所以我正在创建一个习惯跟踪器,每次您在viewHolder上选中一个复选框时都会添加一个值,我遇到的问题是,当我在列表中有6个或更多习惯时,这些复选框无法正常工作(他们开始检查再按随机顺序),并给我上面的错误。

我在回收适配器上使用它:

    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position){
        holder.nombre.setText(mHabitosList.get(position).getNombre());


          FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
          mDatabaseReference = mDatabase.getReference("Habitos");
          FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
          if(user != null) {
              userId = user.getUid();
              habitosReference = mDatabaseReference.child(userId);

              habitosReference.child(mHabitosList.get(holder.getAdapterPosition()).getHabitosId()).addValueEventListener(new ValueEventListener() {
                  @Override
                  public void onDataChange(DataSnapshot dataSnapshot) {
                        Habitos habitos = dataSnapshot.getValue(Habitos.class);
                        if(habitos != null) {
                            updateHabitosTotal[position] = habitos.getTotal();
                            checkBox1 = habitos.isCheckbox1();
                            checkBox2 = habitos.isCheckbox2();
                            checkBox3 = habitos.isCheckbox3();
                            checkBox4 = habitos.isCheckbox4();
                            checkBox5 = habitos.isCheckbox5();
                            checkListeners(holder, position);

                        }

                  }

                  @Override
                  public void onCancelled(DatabaseError databaseError) {

                  }
              });
    }  

在我拥有的检查侦听器上

private void checkListeners(@NonNull final MyViewHolder holder) {
        holder.CB1.setChecked(checkBox1);
        holder.CB1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                {
                    if (holder.getAdapterPosition() != RecyclerView.NO_POSITION) {
                        String HabitosId = mHabitosList.get(holder.getAdapterPosition()).getHabitosId();
                        int total = mHabitosList.get(holder.getAdapterPosition()).getTotal();
                        if (b) {
                            habitosReference.child(HabitosId).child("Checks").child(mDays[0]).setValue(true);
                            total++;
                        } else {
                            habitosReference.child(HabitosId).child("Checks").child(mDays[0]).setValue(false);
                            total--;
                        }
                        habitosReference.child(HabitosId).child("total").setValue(total);
                    }
                }
            }
        });
}
.... And the same for CB2,CB3,CB4,CB5..

我对代码转储表示歉意...任何帮助将不胜感激! (:

代码错误:

  

E / UncaughtException:java.lang.ArrayIndexOutOfBoundsException:   长度= 20;指数= -1           在com.albot.focus.Adapters.HabitosAdapter $ 5.onCheckedChanged(HabitosAdapter.java:224)

2 个答案:

答案 0 :(得分:3)

文档中已经提到getAdapterPosition()可以返回-1或RecyclerView.NO_POSITION,如果

  1. 项目已从适配器中移除。
  2. RecyclerView.Adapter.notifyDataSetChanged()在 最后一次布局。
  3. ViewHolder已被回收。

为避免出现这种情况,单击项目时应始终检查是否getAdapterPosition() != RecyclerView.NO_POSITION

答案 1 :(得分:0)

由于您没有给出太多代码或错误,因此我将做出有根据的猜测。

您的问题可能出在位置变量上,您需要输出位置信息。 mHabitList的索引为0,从0开始。我的猜测是位置不正确,请尝试从位置中减去1。