Skip to content
Advertisement

Unable to add more than one item in a listview

I am working on a tasks app, for which I created a list view that shows list items consitsting of task names, their priority etc. The data given to the list view is from an sqlite database. I, however, am unable to add more than one item to the list. I have no idea why. I have created a method to do so, but it doesn’t seem to work. I don’t know if the error is due to the database or my method itself. Even debugging didn’t help. Please note that I am using a list adapter since I am using a custom listview.

Code for Activity where list is shown :

JavaScript

Code for Adapter Class :

JavaScript

XML code for listview activity :

JavaScript

EDIT : Database Code

JavaScript

Advertisement

Answer

The problem is that you’re creating a new ArrayList while the adapter is left using the old one. That’s why notifyDataSetChanged() doesn’t work because the adapter’s backing list has not changed.

To fix this, update the values list directly

JavaScript

or, add() through the adapter itself.

JavaScript

Even if I add one item, it shows 2 (both the same)

It seems you’re adding the new element twice:

JavaScript

Just add via adapter only as it notifies as well. Check other places too for such duplicates.

Advertisement