This a continuation of my Android: Using An Adapter For ListView post. So please read that first you if haven’t done yet. The projects files for this tutorial is also there.

Adding The Dynamic Functionality

To add the dynamic functionality for the ListView we will need to add 2 functions for adding and removing items in our class that implemented the BaseAdapter. For the function of adding strings:

public void addString( String val ) {
 m_data.add(val);
 notifyDataSetChanged();
}

Aside from just normally adding the string to our data container called m_data. We are also calling the notifyDataSetChanged(), this function will notify the View where are our Adapter is connected. For this instance we will notify the ListView that the underlying data has changed and it must refresh its display/content.

Removing items is also the same, we will remove it normally from the List and notify the ListView of the changes done on the data.

public boolean removeString( int location ) {
 if ( location >= m_data.size() ) {
 return false;
 }
 m_data.remove(location);
 notifyDataSetChanged();
 return true;
}

We also added the location of the item that we need to remove. The important part of these 2 function is the line where we call the notifyDataSetChanged() since without calling it your view will not refresh and show the changes you’ve done.