Bernard Bernard - 1 year ago 108
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();

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download