Bernard Bernard - 7 months ago 46
Android Question

Removing last item of a listview

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() {
public void onClick(View v) {
int count = adapter.getCount();


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.