DOC PREVIEW
SDSU CS 696 - Lists

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 696 Mobile Phone Application DevelopmentFall Semester, 2009Doc 8 ListsSept 24, 2009Copyright ©, 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.References2The Busy Coder's Guide to Android Development, V2.1, Mark L. MurphyAndroid List ExamplesRun Configurations3Lists4public 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)); }}Layout5<?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 setContentViewAdapter Pattern6ArrayAdapter7setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));We have an array of StringsListView uses a list of ViewsArrayAdapter Adapts (converts) what we have to what ListView needsCreates a view for each element in the array8ClientTargetrequest()AdapteespecificRequest()Adapterrequest()adapteeadaptee->specificRequest()Object AdapterOther Adapters9CursorAdapterConverts database cursor for display in selection viewSimpleAdapterConverts XML resourcesCreating an Adapter10public 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)); }}UpperCaseArrayAdapter11public 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); }}Using Resources12public 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); }}Strings.xml13<?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>Single Choice with Text Filter14public 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)); }}MultiSelection15public 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.MultiSelection16Expandable List17The Activity18public 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); }AvatarListAdapter Data19private 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


View Full Document

SDSU CS 696 - Lists

Download Lists
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lists and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lists 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?