Android - List
Objective #1: Understand the basics of a list
- In almost every application, a list is used. A list is an array that is used to display a collection of different items in a scrollable format.
- Each item that is placed in a list has a different index. An index is the memory location of where the item can be found. Lists start their indexes at 0.
- This can be useful to access or modify an item in a list by calling upon their index.
- Unlike sets, lists allow for duplicate items. Lists are less unique and are a broader way to display a collection of items.
- See this link for more info: http://www.vogella.de/articles/Android/ar01s05.html
Objective #2: Why use a list?
- A list will allow you to easily show a multitude of items. This will allow the programmer to have more variables on one screen that will then be able to connect to other variables or even other screens. Lists are a very useful tool in Android Development. A list can have two items or a hundred items. Android makes it easy to view the large lists by incorporating a scroll view in with the list view. Unlike a linear layout, a list view is a good way to have options and preferences.
- Although list views are good for organizing items, lists also have their downfalls. List views cannot be used to show buttons nor can it be incorporated as the main screen for a game app. It is, however, useful for the options or information of a game, or choosing a level of a game.
Objective #3: Understand the essentials of programming a list
- In order to full use the capabilities of a list, you need to import the packages that allow you to use certain code and certain features of a list. These packages that you need to import to use the List feature of Android are:
- android.app.ListActivity
- android.os.Bundle
- android.view.View
- android.widget.ArrayAdapter
- android.widget.ListView
- android.widget.Toast
- Because a list is an array, you need to assign names to the certain indexes of the array. To do so, you need to create an array of strings that will be used as the name of the indexes and that are the options in the list.
- String[ ] example = new String[ ] {one, two, three, four};
- Just as a review, what is the size of this array?
- By using the setListAdapter method, you set the values of the ListActivity. This method is considered as a “bridge” that connects a ListActivity and the data that is supporting it.
- When no data is chosen to be in a list, it is considered empty.
Objective #4: The basics of programming a ListView
- The majority of lists used in today’s world, are not in fact lists. They are called ListViews. A ListView is a view that is used to sort items in a scrollable format.
- See Demo ListView for the complete overview for a ListView
- In order to program a ListView, you have to extend ListActivity instead of Activity. A ListActivity is an activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item. By extending ListActivity, the programmer can now use the majority of the methods inside of the ListActivity that are otherwise not included in the normal Activity.
- To begin programming a ListView, you have to program the basic format in the XML window. You have to program a simple TextView in this step; you will have to perform this step in a separate XML file in the res/layout folder of your project.
- By using the setListAdapter, you will have created a ListView.
- A very interesting method, setTextFilterEnabled(true), allows the user to search for their item in the list and it will eliminate those items that do not match. This method is usually enabled prior to enabling it.