1/23

Lecture Mobile Application Development (119310)

Android Framework Basics

2/23

Agenda

3/23

ListActivity

  • Derives from Activity class
  • Holds a ListView that can be bound to different data sources, e.g.
    • Arrays
    • Database Cursors (containing query results)
    • other, e.g. XML files

4/23

ListView

  • A ListView shows items in 
a scrollable list
  • The items are defined
by an Adapter which is
also responsible for the layout and rendering of the items in the list
  • Each item is selectable
5/23

Adapter

  • The ListView delegates the presentation of items to the corresponding Adapter
  • The Android Framework already provides us with implementations of 
the Adapter interface
    • ArrayAdapter
    • CursorAdapter
    • SimpleAdapter
6/23

Adapter Implementation Examples

7/23

ArrayAdapter

final List<String> bandlist = asList("Deichkind", "Metallica","Queen","U2");
		
ArrayAdapter<String> bandArray = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,bandlist);

setListAdapter(bandArray);

8/23

SimpleAdapter

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
   <TextView
        android:id="@+id/item1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
   <TextView
        android:id="@+id/item2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
   <TextView
        android:id="@+id/item3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

9/23

Simple Adapter

10/23

Framework Components Part IV

Content Provider

11/23

Content Provider

12/23

MediaStore

_ID ARTIST TITLE DATA DISPLAY_NAME DURATION
12 Lithium Dawn Everything Is Zero storage/sdcard/01 01 Everything Is Zero.mp3 01 01 Everything Is Zero.mp3 396720
13 Blur Fool’s Day storage/sdcard/Blur - Fool´s Day.mp3 Blur - Fool´s Day.mp3 207047
14 pornophonique sad robot storage/sdcard/sad robot.mp3 sad robot.mp3 312059
13/23

Content Resolver

ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(
		        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, //URI to CP
		        projection, //Array defining the data to select
		        selection, //String defining the criteria of the data selection
		        selectArgs, //parametes for the data selection
		        sortOrder); //specifies the result order
14/23

Content Resolver Parameters

15/23

Content Resolver Parameters

String selection = MediaStore.Audio.Media.ARTIST + " LIKE ?";
16/23

Content Resolver Parameters

String[] selectionArgs = new String[]{"blur"};
17/23

Content Resolver Parameters

String[] selectionArgs = new String[]{"blur"};
String sortOrder = MediaStore.Audio.Media.DURATION;
18/23

Content Resolver

Parameter Description
URI The URI, using the content:// scheme, for the content to retrieve.
Projection A list of which columns to return. Passing null will return all columns, which is inefficient.
Selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
19/23

Cursor

final List<String> songs = new ArrayList<String>();

while(cursor.moveToNext()){		                                                 		     

	songs.add(cursor.getString(1));

}
20/23

SimpleCursorAdapter

ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
	new String[]{MediaStore.Audio.Media._ID,MediaStore.Audio.Media.ARTIST,
    			MediaStore.Audio.Media.ALBUM}, null, null, null);
//define which content should be placed where in the layout
String[] from = new String[]{MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.ALBUM};
int[] to = new int[]{R.id.artist,R.id.album};

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.artist_list,
		cursor, from, to, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
//artistList is the ListView object
artistList.setAdapter(adapter);
21/23

Android Assignment

22/23

Summary

23/23

Recab Questions