简体   繁体   English

每当我想删除 RecyclerView 项目时出现 IndexOutOfBoundsException

[英]IndexOutOfBoundsException whenever I want to delete an RecyclerView item

I manage to make a list with RecyclerView and now I want to delete a row by clicking on that row.我设法用 RecyclerView 制作了一个列表,现在我想通过单击该行来删除该行。 But when I click on it, nothing happens and when I click again I get IndexOutOfBoundsException .但是当我点击它时,没有任何反应,当我再次点击时,我得到了IndexOutOfBoundsException

I am struggling to find the exception...我正在努力寻找例外......

This is my MainActivity.class这是我的MainActivity.class

MyRecyclerViewAdapter adapter;
    ArrayList<ExampleItem> movieList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // data to populate the RecyclerView with
        movieList = new ArrayList<>();
        movieList.add(new ExampleItem("Dr. No", "1962", "Sean Connery"));
        movieList.add(new ExampleItem("From Russia with Love", "1963", "Sean Connery"));
        movieList.add(new ExampleItem("Goldfinger", "1964", "Sean Connery"));
        movieList.add(new ExampleItem("Thunderball", "1965", "Sean Connery"));
        movieList.add(new ExampleItem("You Only Live Twice", "1967", "Sean Connery"));

        RecyclerView recyclerView = findViewById(R.id.homeRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyRecyclerViewAdapter(this, movieList);
        adapter.setClickListener(this); //MyRecyclerViewAdapter.ItemClickListener muss implementiert!
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(View view, int position) {
        movieList.remove(position);
    }

And here is my Adapter class (I shortened the code)这是我的适配器 class(我缩短了代码)

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private List<ExampleItem> mList;
    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;

    // data is passed into the constructor
    MyRecyclerViewAdapter(Context context, List<ExampleItem> data) { 
        this.mInflater = LayoutInflater.from(context);
        this.mList = data; //List is passed and is set into the variable above
    }

    // inflates the row layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_items, parent, false);
        return new ViewHolder(view);
    }

    // binds the data to the TextView in each row
    @Override //Pass the value with this method:
    public void onBindViewHolder(ViewHolder holder, int position) {
        ExampleItem currentItem = mList.get(position);

        holder.titleView.setText(currentItem.getText1());
        holder.yearView.setText(currentItem.getText2());
        holder.actorView.setText(currentItem.getText3());
    }

    // total number of rows
    @Override
    public int getItemCount() {
        return mList.size(); 
    }

    //RecyclerView.ViewHolder
    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public final View mView;
        TextView titleView;
        TextView yearView;
        TextView actorView;

        ViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            titleView = itemView.findViewById(R.id.title);
            yearView = itemView.findViewById(R.id.year);
            actorView = itemView.findViewById(R.id.actor);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    // allows clicks events to be caught
    void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener { 
        void onItemClick(View view, int position);
    }
}

Do I have to update the list after deleting an item maybe?删除项目后是否必须更新列表?

Replace:代替:

@Override
public void onItemClick(View view, int position) {
    movieList.remove(position);
}

With:和:

@Override
public void onItemClick(View view, int position) {
    movieList.remove(position);
    adapter.notifyItemRemoved(position);
}

In the onClickListener try adding在 onClickListener 尝试添加

    adapter.notifyDataSetChanged();

notifyDataSetChanged example notifyDataSetChanged 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击RecyclerView项时出现IndexOutOfBoundsException - IndexOutOfBoundsException when clicking RecyclerView item java.lang.IndexOutOfBoundsException :当我尝试从 recyclerview 列表中删除项目时 - java.lang.IndexOutOfBoundsException : When i try to remove item from list of recyclerview 无法删除recyclerview项目 - Unable to delete recyclerview item RecyclerView单击删除项目 - RecyclerView delete item on clicking it 当我单击RecyclerView项时,我想获取ID - I want to get the id when I click a RecyclerView item 我想从列表视图中删除项目 - I want to delete an item from a list view 当我单击 RecyclerView 项目时,我希望 TextView 在同一活动中显示项目的标题 - When I click on RecyclerView Item I want the TextView in same activity to display Item's title RecyclerView 中 MapView 的 IndexOutOfBoundsException - IndexOutOfBoundsException for MapView in RecyclerView 每当我尝试获取表上的值并在 `setString` 上使用它时,我都会收到 indexOutOfBoundsException - I am getting indexOutOfBoundsException whenever I try to get the value on my table and use it on `setString` 滚动滚动获取RecyclerView异常:-“ java.lang.IndexOutOfBoundsException:检测到不一致。 无效的商品位置21” - On scroll the RecyclerView getting exception : -“ java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 21”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM