www hndit com HNDIT 2417 Mobile Application Development
www. hndit. com HNDIT 2417 Mobile Application Development Week 7 List. View
www. hndit. com List. View
www. hndit. com List. View • List. View is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
www. hndit. com List. View
www. hndit. com Adapter Android Adapter is a bridge between the View (e. g. List. View) and the underlying data for that view. An adapter manages the data and adapts the data to the individual rows (list. Items) of the view.
www. hndit. com Adapter • Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an Adapter. View ( i. e. List. View or Grid. View). The common adapters are Array. Adapter, Base. Adapter, Cursor. Adapter, Simple. Cursor. Adapter, Spinner. Adapter and Wrapper. List. Adapter.
www. hndit. com Array. Adapter • You can use this adapter when your data source is an array. By default, Array. Adapter creates a view for each array item by calling to. String() on each item and placing the contents in a Text. View. Consider you have an array of strings you want to display in a List. View, initialize a new Array. Adapter using a constructor to specify the layout for each string and the string array
www. hndit. com Construction of Array. Adapter • Array. Adapter adapter = new Array. Adapter<String>(this, R. layout. List. View, String. Array); • Here arguments for this constructor − – First argument this is the application context. Most of the case, keep itthis. – Second argument will be layout defined in XML file and having. Text. View for each string in the array. – Final argument is an array of strings which will be populated in the text view.
www. hndit. com • Once you have array adapter created, then simply call set. Adapter() on your List. View object as follows − List. View list. View = (List. View) find. View. By. Id(R. id. listview); list. View. set. Adapter(adapter); You will define your list view under res/layout directory in an XML file.
Example- List. Display. java www. hndit. com
activity_main. xml www. hndit. com
Layout for a single row of list. View www. hndit. com
Output www. hndit. com
- Slides: 13