Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I manage to make a list with RecyclerView and now I want to delete a row by clicking on that row. But when I click on it, nothing happens and when I click again I get IndexOutOfBoundsException.

I am struggling to find the exception...

This is my 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)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
395 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...