Android中IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:1).state:4
2021/6/16 18:26:11
本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享