Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Day 8: Dealing with Lists and ListViews


Published on

Published in: Technology, Art & Photos
  • Login to see the comments

Day 8: Dealing with Lists and ListViews

  1. 1. Android ApplicationDevelopmentList Activity & List View
  2. 2. List View●Android provides the view "List View" which is capable ofdisplaying a scrollable list of items.●"ListView"gets the data to display via an adapter. An adapter whichmust extend "Base Adapter" and is responsible for providing the datamodel for the list and for converting the data into the fields of thelist.●Android has two standard adapters- Array Adapter-Cursor Adapter● "Array Adapter" can handle data based on Arrays or Lists while"SimpleCursorAdapter" handle database related data. You candevelop your own Adapter by extending these classes or the BaseAdapter class.
  3. 3. List Activity●You can directly use the "List View" in your layout as any other UIcomponent. In case your Activity is primary showing a list you canextend the activity "List Activity" which simplifies the handling of a"List View".● "List Activity" extends "Activity" and provides simplified handling oflists. For example you have a predefine method if someone clicks ona list element.●"List Activity" contains a "List Adapter" which is responsible formanaging the data. This adapter must be set in the onCreate()method of your Activity via the method setListAdapter().●If the user select in the list a list entry the method onListItemClick()will be called. This method allows to access the selected element.
  4. 4. List Activity●Android provides already some default layouts which you can use inyour Adapter, e.g.-"android.R.layout.simple_list_item1". In case you dont want to useone of the pre-defined layouts your own layout must have anelement with the id "@android:id/list" which is the ListView.-You can also use a view with the id "@android:id/empty". This viewis displayed if the list is empty. For example you could display herean error message.
  5. 5. ListViews and performance●Displaying a large dataset must be efficiently implemented on amobile device. Therefore the ListView only creates views (widget) ifneeded and attach them to the view hierarchy.●The default Adapter implementation for a ListView will recycleviews, e.g. if a row is not displayed anymore it will be recycled andonly its content will change.● If you implement your own adapter for a view you also should dothis to avoid performance problems.
  6. 6. ListActivity with ArrayAdapter and Androidstandard layout●Create a new Android project "com.basistraining.listactivity" withthe activity "MyList".●You do not need to change the default layout "main.xml". Create thefollowing activity.public class MyList extends ListActivity {/** Called when the activity is first created. */public void onCreate(Bundle icicle) {super.onCreate(icicle);// Create an array of Strings, that will be put to our ListActivityString[] names = new String[] { "Bangladesh", "India", "China", "Japan","Denmark", "Australia", "Germany", "Indonesia"}// Create an ArrayAdapter, that will actually make the Strings above// appear in the ListViewthis.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names));}
  7. 7. ListActivity with ArrayAdapter and Androidstandard layout@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id);// Get the item that was clickedObject o = this.getListAdapter().getItem(position);String keyword = o.toString();Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();}
  8. 8. ListActivity with ArrayAdapter and Androidstandard layout
  9. 9. ListActivity with own layout●You can also define your own layout for the rows and assign thislayout to your row adapter. We will add a graphic to each list entry.●Create the following layout file "rowlayout.xml" in the res/layoutfolder of your project"com.basistraining.listactivity".
  10. 10. ListActivity with own layout●Change your activity "MyList" to the following. This is almost thesame coding as in the previous example,●The only difference is that we are using our own layout in theArrayAdapter and telling the adapter which UI element shouldcontains the text.
  11. 11. ListActivity with own layout
  12. 12. ListActivity with own layout
  13. 13. ListActivities with flexible layout●The following uses an image "no.png". I placed it in the "res/drawable-mdpi" folder. You must maintain your own icon. In the easiest case justcopy "icon.png" to "no.png" and use a drawing program to change it a littlebit.●If you want to influence the display of the different rows you can defineyour own adapter and override the getView() method. This method isresponsible for creating the individual rows of your "ListView". getView()need to return a View (containing several others) for each row.●For this read the pre-defined layout via the class "LayoutInflator" andreturn one individual view per row. We extend ArrayAdapter but we couldalso directly implement "BaseAdapter“●If "convertView" is not null we re-used this view. Android recycles rows(views) which are not displayed anymore. Using exsting rows savesmemory and CPU consumption.
  14. 14. ListActivities with flexible layout●Our implementation will also use the so-called "ViewHolder" pattern. Themethod findViewById() is a expensive operation, therefore we shouldavoid doing this operation if not necessary.●The ViewHolder stores a reference to the required views in a row. ThisViewHolder is then attached to the row via the method setTag().●Every view can get a tag assigned. If the row is recycled we can get theViewHolder via getTag() method.●This seems like a lot of overhead but is much faster then the repetitivecall of findViewById().
  15. 15. ListActivities with flexible layout●We still using the project "com.basistraining.listactivity". Create thefollowing class "".
  16. 16. ListActivities with flexible layout
  17. 17. ListActivities with flexible layout●Now In the MyList class we write the following code
  18. 18. ListActivities with flexible layout
  19. 19. Rows interacting with the data model●Your row layout can also contain views which interact with theunderlying data model.●For example you can have a "Checkbox" view in your row and if thecheckbox is selected you change the data which is displayed in therow.●We still use the same project. Create a new row layout"rowbuttonlayout.xml“.
  20. 20. Rows interacting with the data model
  21. 21. Rows interacting with the data model●create for this example the class "Model" which hold the nameand the information if this element is currently selected.
  22. 22. Rows interacting with the data model●Create the following Adapter. This adapter will add a listener on theCheckbox. If the checkbox is selected the underlying data of themodel is also changed. Search Checkbox gets its model elementassigned via the setTag() method.
  23. 23. Rows interacting with the data model
  24. 24. Rows interacting with the data model●Finally change your "ListView" to the following.
  25. 25. Rows interacting with the data model●If you start your app you should be able to flag items. Thesechanges will be reflected in your model.