Shopping List Example App

This application displays a list of items with the ability to add items to the list using an EditText widget. The list is displayed using a ListView, which obtains the data for the list using an Adapter class. Often a custom Adapter will be created and used for managing items in a list, but we will use the provided ArrayAdapter class. Items can be added to or removed from this ArrayAdapter similar to a normal array, but each time the content in the ArrayAdapter changes the ListView will create or remove Views accordingly.

The EditText widget is a View that you can type into to change the text. In this application a Button widget next to the EditText can be pressed to add the current contents of the EditText to the ListView. To add this functionality an OnClickListener must be registered to the Button. An OnClickListener is simply a class that contains a public void onClick(View v) method to be used as a callback whenever the Button is clicked. For simplicity the Activity class implements the OnClickListener interface.

The layout of this application contains several Views arranged within a RelativeLayout. By using RelativeLayout additional alignment options are available for arranging Views, for example in this application the Button is placed on the right side of the screen using the layout_alignParentRight attribute and the EditText is placed to the left of the Button using the layout_toLeftOf attribute.

Download the source of the project by clicking here.

To modify the application you will be adding the ability to remove items from the list. After completing this task items can be removed by long clicking on an item on the list and selecting “Remove” from the menu that appears. This will require the addition of three new methods.