Top Banner
CS 696 Mobile Phone Application Development Fall Semester, 2009 Doc 8 Lists Sept 24, 2009 Copyright ©, All rights reserved. 2009 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http:// www.opencontent.org/opl.shtml) license defines the copyright on this document.
23

CS 696 Mobile Phone Application Development Fall Semester ...

May 10, 2015

Download

Documents

peterbuck
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 696 Mobile Phone Application Development Fall Semester ...

CS 696 Mobile Phone Application DevelopmentFall Semester, 2009

Doc 8 ListsSept 24, 2009

Copyright ©, All rights reserved. 2009 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.

Page 2: CS 696 Mobile Phone Application Development Fall Semester ...

References

2

The Busy Coder's Guide to Android Development, V2.1, Mark L. Murphy

Android List Examples

Page 3: CS 696 Mobile Phone Application Development Fall Semester ...

Run Configurations

3

Page 4: CS 696 Mobile Phone Application Development Fall Semester ...

Lists

4

public class SelectionExamples extends ListActivity { String[] items = { "Gautama Buddha", "Kalki", "Krishna", "Kurma", "Matsya", "Narasimha", "Parashurama", "Rama", "Vamana", "Varaha" };

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)); }}

Page 5: CS 696 Mobile Phone Application Development Fall Semester ...

Layout

5

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/selection" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/list_title"/> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" /> </LinearLayout>

If the list view has id "@android:id/list" the ListActivity will find it without having to call setContentView

Page 6: CS 696 Mobile Phone Application Development Fall Semester ...

Adapter Pattern

6

Page 7: CS 696 Mobile Phone Application Development Fall Semester ...

ArrayAdapter

7

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));

We have an array of Strings

ListView uses a list of Views

ArrayAdapter Adapts (converts) what we have to what ListView needsCreates a view for each element in the array

Page 8: CS 696 Mobile Phone Application Development Fall Semester ...

8

Client Targetrequest() Adaptee

specificRequest()

Adapter

request()adaptee

adaptee->specificRequest()

Object Adapter

Page 9: CS 696 Mobile Phone Application Development Fall Semester ...

Other Adapters

9

CursorAdapterConverts database cursor for display in selection view

SimpleAdapterConverts XML resources

Page 10: CS 696 Mobile Phone Application Development Fall Semester ...

Creating an Adapter

10

public class SelectionExamples extends ListActivity { String[] items = { "Gautama Buddha", "Kalki", "Krishna", "Kurma", "Matsya", "Narasimha", "Parashurama", "Rama", "Vamana", "Varaha" };

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new UpperCaseArrayAdapter(this, android.R.layout.simple_list_item_1, items)); }}

Page 11: CS 696 Mobile Phone Application Development Fall Semester ...

UpperCaseArrayAdapter

11

public class UpperCaseArrayAdapter extends ArrayAdapter<String> { private Context mContext; private String[] mElements; public UpperCaseArrayAdapter(Context context, int resource, String[] elements) { super(context, resource, elements); mContext = context; mElements = elements; }

public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = new TextView(mContext); } ((TextView) convertView).setText(mElements[position].toUpperCase()); return (convertView); }}

Page 12: CS 696 Mobile Phone Application Development Fall Semester ...

Using Resources

12

public class SelectionExamples extends ListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.avatars, android.R.layout.simple_list_item_1); setListAdapter(adapter); }}

Page 13: CS 696 Mobile Phone Application Development Fall Semester ...

Strings.xml

13

<?xml version="1.0" encoding="utf-8"?><resources> <string name="list_title">Ten avatars</string> <string name="app_name">Selection Examples</string> <string-array name="avatars"> <item>Gautama Buddha</item> <item>Kalki</item> <item>Krishna</item> <item>Kurma</item> <item>Matsya</item> <item>Narasimha</item> <item>Parashurama</item> <item>Rama</item> <item>Vamana</item> <item>Varaha</item> </string-array></resources>

Page 14: CS 696 Mobile Phone Application Development Fall Semester ...

Single Choice with Text Filter

14

public class SelectionExamples extends ListActivity { String[] items = { "Gautama Buddha", "Kalki", "Krishna", "Kurma", "Matsya", "Narasimha", "Parashurama", "Rama", "Vamana", "Varaha" }; TextView selection;

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, items)); selection = (TextView) findViewById(R.id.selection); getListView().setTextFilterEnabled(true); }

public void onListItemClick(ListView parent, View v, int position, long id) { selection.setText((String)getListView().getItemAtPosition(position)); }}

Page 15: CS 696 Mobile Phone Application Development Fall Semester ...

MultiSelection

15

public class SelectionExamples extends ListActivity { String[] items = { "Gautama Buddha", "Kalki", "Krishna", "Kurma", "Matsya", "Narasimha", "Parashurama", "Rama", "Vamana", "Varaha" }; TextView selection;

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items)); selection = (TextView) findViewById(R.id.selection); getListView().setTextFilterEnabled(true); getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); }

public void onListItemClick(ListView parent, View v, int position, long id) { selection.setText((String)getListView().getItemAtPosition(position)); }}

When you want to find out all the items that were selected you need to call getCheckedItemPositions() on the list view.

Page 16: CS 696 Mobile Phone Application Development Fall Semester ...

MultiSelection

16

Page 17: CS 696 Mobile Phone Application Development Fall Semester ...

Expandable List

17

Page 18: CS 696 Mobile Phone Application Development Fall Semester ...

The Activity

18

public class SelectionExamples extends ListActivity {

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new AvatarListAdapter(this)); }

protected void onListItemClick(ListView l, View v, int position, long id) { ((AvatarListAdapter) getListAdapter()).toggle(position); }

Page 19: CS 696 Mobile Phone Application Development Fall Semester ...

AvatarListAdapter Data

19

private class AvatarListAdapter extends BaseAdapter { private Context mContext;

private String[] mAvatars = { "Gautama Buddha", "Kalki", "Krishna", "Kurma", "Matsya" };

private String[] mAvatarDescription = { " Returned pure dharma to the world", " The Destroyer of foulness", " Represents a person in more practical society", " The tortoise, represents a human embryo just growing tiny legs, with a huge belly", " The fish, appeared in the Satya Yuga. Represents beginning of life" };

private boolean[] mExpanded = { false, false, false, false, false };

Page 20: CS 696 Mobile Phone Application Development Fall Semester ...

AvatarListAdapter Accessors

20

public AvatarListAdapter(Context context) { mContext = context; }

public int getCount() { return mAvatars.length; }

public Object getItem(int position) { return position; }

public long getItemId(int position) { return position; }

Page 21: CS 696 Mobile Phone Application Development Fall Semester ...

AvatarListAdapter getView

21

public View getView(int position, View convertView, ViewGroup parent) { AvatarView avatar; if (convertView == null) { avatar = new AvatarView(mContext, mAvatars[position], mAvatarDescription[position], mExpanded[position]); } else { avatar = (AvatarView) convertView; avatar.setTitle(mAvatars[position]); avatar.setDialogue(mAvatarDescription[position]); avatar.setExpanded(mExpanded[position]); } return avatar; }

public void toggle(int position) { mExpanded[position] = !mExpanded[position]; notifyDataSetChanged(); } }

Page 22: CS 696 Mobile Phone Application Development Fall Semester ...

AvatarView

22

private class AvatarView extends LinearLayout {

private TextView mTitle; private TextView mDescription;

public void setTitle(String title) { mTitle.setText(title); }

public void setDialogue(String words) { mDescription.setText(words); }

public void setExpanded(boolean expanded) { mDescription.setVisibility(expanded ? VISIBLE : GONE); }

Page 23: CS 696 Mobile Phone Application Development Fall Semester ...

AvatarView Constructor

23

public AvatarView(Context context, String title, String dialogue, boolean expanded) { super(context);

this.setOrientation(VERTICAL);

mTitle = new TextView(context); mTitle.setText(title); addView(mTitle, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

mDescription = new TextView(context); mDescription.setText(dialogue); addView(mDescription, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

mDescription.setVisibility(expanded ? VISIBLE : GONE); }