Problem with updating recycler view



  • Problem is, I have an adapter. recycler viewTwo methods:

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.mCheckBox.setChecked(mTodoItems.get(position).isChecked());
        holder.mEditText.setText(mTodoItems.get(position).getString());
        holder.mRemoveImageView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v){
                mTodoItems.remove(position);
                notifyDataSetChanged();
            }
        });
        holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
                mTodoItems.get(position).setChecked(isChecked);
            }
        });
        holder.mEditText.addTextChangedListener(new TextWatcher() {
            @Override public void beforeTextChanged(CharSequence s, int start, int count, int after){
    
        }
    
        @Override public void onTextChanged(CharSequence s, int start, int before, int count){
    
        }
    
        @Override public void afterTextChanged(Editable s){
            mTodoItems.get(position).setString(s.toString());
        }
    });
    

    }

    public void addItem(TodoItem todoItem){
    mTodoItems.add(todoItem);
    notifyItemInserted(mTodoItems.size()-1);
    }

    If the elements are less than six, I think it's okay, otherwise the IndexOutOfBoundsException leaves. As long as I'm trying to find a problem, it's not working. Did you see who was?

    FATAL EXCEPTION: main
    Process: com.kapmayn.artyom.kitchen, PID: 27129
    java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
    at java.util.ArrayList.get(ArrayList.java:308)
    at com.kapmayn.artyom.kitchen.adapters.TodoListRecyclerAdapter$3.afterTextChanged(TodoListRecyclerAdapter.java:61)
    at android.widget.TextView.sendAfterTextChanged(TextView.java:8004)
    at android.widget.TextView.setText(TextView.java:4356)
    at android.widget.TextView.setText(TextView.java:4204)
    at android.widget.EditText.setText(EditText.java:84)
    at android.widget.TextView.setText(TextView.java:4179)
    at com.kapmayn.artyom.kitchen.adapters.TodoListRecyclerAdapter.onBindViewHolder(TodoListRecyclerAdapter.java:39)
    at com.kapmayn.artyom.kitchen.adapters.TodoListRecyclerAdapter.onBindViewHolder(TodoListRecyclerAdapter.java:22)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2625)
    at android.view.View.measure(View.java:18796)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
    at android.view.View.measure(View.java:18796)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
    at android.view.View.measure(View.java:18796)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
    at android.view.View.measure(View.java:18796)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1107)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:631)
    at android.view.View.measure(View.java:18796)



  • holder.mEditText.addTextChangedListener is trouble. At the time of creation, the position shows the current condition, but if the element is the last and the previous one has been removed, the index of the last element is already position-1 and the treatment mTodoItems.get(position).setString(s.toString()) will extinguish the error.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2