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

It shows no error but the app crash when i click the button to remove the last item in the list view. List view works fine with the arraylist... just when i want to remove the last item, it gives me a crash.

btnUndo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int count = adapter.getCount();
            adapter.remove(adapter.getItem(count));
            adapter.notifyDataSetChanged();
        }
    });
See Question&Answers more detail:os

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

1 Answer

Arrays are 0-based, so you should do the following:

adapter.remove(adapter.getItem(count - 1));

I also suggest to use notifyItemRemoved instead of notifyDataSetChanged.


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