SyntaxHighlighter

Friday, September 24, 2010

Android Programming Jumpstart – 6

image
  This is lab 6 in my ongoing series of jumpstart Android programming tutorials.
  This lab will focus on selectable ListViews.


Here are some terms that you’ll need to understand to work with ListViews:

  • Context
    • Interface to information about an application environment
    • Typically when a context parameter is needed for your app you will use (this)
  • Adapters
    • The bridge between a View and the data for that viewimage
    • Provides access to the data items
  • ArrayAdapter
    • The ArrayAdapter constructor takes three parameters:
      • The Context to use (this)
      • The resource ID of a view to use
      • The actual array or list of items to show
    • By default, the ArrayAdapter will invoke toString() on the objects in the list and wrap each of those strings in the view designated by the supplied resource.
    • android.R.layout.simple_list_item_1
      • a built in resource that turns those strings into TextView objects styled in a particular way.
    • Definitions for some built in resources can be found here
  • Listeners
    • Interface to setup callbacks for actions on your widgets
    • Use to set up link between a button and onClick

  • ListView
    • To use you will need to define one in your layout
      • android.R.layout.simple_list_item_1
      • android.R.layout.simple_list_item_single_choice
      • android.R.layout.simple_list_item_multiple_choice
    • Assign data to it using setAdapter()
    • Assign functionality to catch users selections using a Listener using setOnItemClickListener()
    • If your View is really just your list, you can make things easier on yourself by having your activity extend ListActivity
      • It will then construct the full screen list for you
      • You must name your ListView @android:id/list

Check out the two different ways of working with a ListView below.  In this first example everything is setup in onCreate().

image

image

In this example everything we have broken it out.
image 

image

Here are some different types of usage for a ListView. 
It is quite versatile and can be used for single or multi selections.
image

No comments:

Post a Comment