C/C++教程

Android中IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:1).state:4

本文主要是介绍Android中IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:1).state:4,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
报错原因

很明显就是数组越界了

在这里插入图片描述

目的

我为什么显示报错,是因为我们想要使用recycleview来实现数据绑定,然后呢,每个item布局里面有个按钮,我想要点击item里面的按钮,然后删除当前的布局。然后我就再adapter里面使用点击事件呗,然后就再Mainactivity里面设置 adaper.notifyDataSetChanged();结果就报标题的错误了
在这里插入图片描述

在这里插入图片描述

解决办法:

在adapter适配器里面写一个监听接口,用于监听点击了哪一项,然后在MainActity中回调该方法,然后每删除一个数据,就调用 adaper.notifyDataSetChanged()
注:如果RecyclerView.Adapter适配器要是可以调用notifyDataSetChanged就好了,就不要回调了

参考代码:

1.适配器类

public class MedicineAdaper extends RecyclerView.Adapter<MedicineAdaper.MyHolder>{
    private Context mcontext;
    List<Medicine> mmedicines;
    private  OnItemClickListerner mlisterner;
    public MedicineAdaper(Context context, List<Medicine> medicines, OnItemClickListerner listerner)  {
        this.mcontext = context;
        this.mmedicines = medicines;
        this.mlisterner = listerner;
    }

    @NonNull

    @Override
    public MedicineAdaper.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(mcontext).inflate(R.layout.item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MedicineAdaper.MyHolder holder, final int position) {
        holder.medicine.setText(mmedicines.get(position).medicine);
        holder.way.setText(mmedicines.get(position).way);
        holder.time.setText(mmedicines.get(position).time);
        Log.d("safsafasfas", mmedicines.get(0).medicine);
        Log.d("safsafasfas", mmedicines.get(0).way);
        Log.d("safsafasfas", mmedicines.get(0).time);
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mlisterner.onClick(position);
            }
        });
    

    }

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


    //ViewHolder
    class  MyHolder extends RecyclerView.ViewHolder{
 private TextView medicine,time,way;
 ImageButton button;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            medicine=itemView.findViewById(R.id.medicine);
            time = itemView.findViewById(R.id.time);
            way = itemView.findViewById(R.id.way);
            button =itemView.findViewById(R.id.button);


        }
    }
    //定义一个接口用于回调操作
    public  interface  OnItemClickListerner{
        void onClick(int pos);
    }

}

2.MainActivity类

package com.mt.recycleviewtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<Medicine> medicines;
    MedicineAdaper adaper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_view_recyclerView();
    }

    /**
     * 初始化控件
     */
    private void init_view_recyclerView() {
        medicines = new ArrayList<Medicine>();
        Medicine medicine1 = new Medicine("8.4","安眠药","口服");
        medicines.add(medicine1);
        Medicine medicine2 = new Medicine("2020.3.4","莫斯利安","口服");
        medicines.add(medicine2);
        Medicine medicine3 = new Medicine("2020.12.5","唇膏","口服");
        medicines.add(medicine3);




        recyclerView=findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(MainActivity.this);
        recyclerView.setLayoutManager(linearLayoutManager);
         adaper = new MedicineAdaper(MainActivity.this, medicines, new MedicineAdaper.OnItemClickListerner() {
            @Override
            public void onClick(int pos) {

                medicines.remove(pos);
                adaper.notifyDataSetChanged();
            }
        });
        recyclerView.setAdapter(adaper);



    }


}

3.medicine类

package com.mt.recycleviewtest;

public class Medicine {
    String time;
    String medicine;
    String way;

    public Medicine(String time, String medicine, String way) {
        this.time = time;
        this.medicine = medicine;
        this.way = way;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getMedicine() {
        return medicine;
    }

    public void setMedicine(String medicine) {
        this.medicine = medicine;
    }

    public String getWay() {
        return way;
    }

    public void setWay(String way) {
        this.way = way;
    }
}

这篇关于Android中IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:1).state:4的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!