• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Day 8: Dealing with Lists and ListViews
 

Day 8: Dealing with Lists and ListViews

on

  • 5,651 views

 

Statistics

Views

Total Views
5,651
Views on SlideShare
897
Embed Views
4,754

Actions

Likes
0
Downloads
87
Comments
0

4 Embeds 4,754

http://droidtraining.wordpress.com 4696
http://droidtraining.wordpress.com. 49
http://webcache.googleusercontent.com 5
https://droidtraining.wordpress.com 4

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Day 8: Dealing with Lists and ListViews Day 8: Dealing with Lists and ListViews Presentation Transcript

    • Android ApplicationDevelopmentList Activity & List View
    • 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.
    • 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.
    • 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.
    • 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.
    • 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));}
    • 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();}
    • ListActivity with ArrayAdapter and Androidstandard layout
    • 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".
    • 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.
    • ListActivity with own layout
    • ListActivity with own layout
    • 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.
    • 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().
    • ListActivities with flexible layout●We still using the project "com.basistraining.listactivity". Create thefollowing class "MyArrayAdapter.java".
    • ListActivities with flexible layout
    • ListActivities with flexible layout●Now In the MyList class we write the following code
    • ListActivities with flexible layout
    • 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“.
    • Rows interacting with the data model
    • 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.
    • 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.
    • Rows interacting with the data model
    • Rows interacting with the data model●Finally change your "ListView" to the following.
    • 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.