Android Programming Lecture 6 List View and Custom View
Android Programming Lecture 6 List View and Custom View
List View Android provides the view "List View"which is capable of displaying a scrollable list of items. Lists are one of the most common designs in mobile apps 11:09 420PM o oScrollable = Someone Like You o Selectable Space Bound 中。么手国四8甲+0 o Programmable to Stranger In Moscow ★★★★★2651 88 bring up the next ove The Way You Lie Tobias Fanke sn由Ckt )340. Activity (screen) wa明Mere Kh明 ★★★★92 946 All My Days 780 Life For Rent
List View • Android provides the view "List View" which is capable of displaying a scrollable list of items. • Lists are one of the most common designs in mobile apps 2 o Scrollable o Selectable o Programmable to bring up the next Activity (screen)
/。1第 Create the List ■Lsisplay Android iPhone windowsMobile Blackberry public class MainActivity extends ListActivity{ webos public void onCreate(Bundle savedInstanceState){ Ubuntu super.onCreate(savedInstanceState); windows7 Max OS X "List Activity"extends "Activity"and provides simplified handling of lists 3
3 public class MainActivity extends ListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // do not call the method setContentView(), as the List Activity has its default layout with a list view embedded // Step 1: Create the data String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" }; // Step 2: Create an Adapter object to convert data to list row ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values); // Step 3: Load the adapter to list view setListAdapter(adapter); } Create the List "List Activity" extends "Activity" and provides simplified handling of lists
Create the List ■ListDisplay Android iPhone WindowsMobile Blackberry public class MainActivity extends ListActivity{ webos public void onCreate(Bundle savedInstancestate){ Ubuntu super.onCreate(savedInstanceState); windows7 /do not call the method setContentview(),as the Max OS X List Activity has its default layout with a list view embedded /Step 1:Create the data String[]values new String[]{"Android","iPhone", "WindowsMobile","Blackberry","Webos","Ubuntu","Windows7","Max OS x", "Linux'","0s/2"; /Step 2:Create an Adapter object to convert data to list row ArrayAdapteradapter new ArrayAdapter(this, android.R.layout.simple list item 1,values); /Step 3:Load the adapter to list view setListAdapter(adapter);
4 public class MainActivity extends ListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // do not call the method setContentView(), as the List Activity has its default layout with a list view embedded // Step 1: Create the data String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" }; // Step 2: Create an Adapter object to convert data to list row ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values); // Step 3: Load the adapter to list view setListAdapter(adapter); } Create the List
Adapter:Man in the middle Data source Adapter ListView Cursor ArrayList ArrayAdapter:use TextView to represent the list items SimpleCursorAdapter:used with SQLite databases and content providers by converting cursor rows to list items 女
Adapter: Man in the middle 5 • ArrayAdapter: use TextView to represent the list items • SimpleCursorAdapter: used with SQLite databases and content providers by converting cursor rows to list items
ArrayAdapteradapter new ArrayAdapter(this, android.R.layout.simplelist item_1,values); 可坐 零145 /6403 B□G1:27AM ndroldListVlew CustomTwoLineListActivity Interiace January Red hello 7 the color red February Green world March Blue the color blue musart April May June July simple_list item1 simple list_item 2 simple_list_item_single_choice 6
6 simple_list_item_1 simple_list_item_2 simple_list_item_single_choice ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);
41108 ■ListDisplay User Interaction in List View Android iPhone WindowsMobile Blackberry User setListAdapter()to inflate Webos the list view using the adapter Ubuntu Windows7 If the user select in the list a list entry Max OSX the method onListItemclick() will be called.This method allows to access the selected element. @Override protected void onListItemClick(ListView 1,View v,int position,long id){ super.onListItemClick(1,v,position,id); /Callback function when an item in the list is clicked /ListView 1:The ListView where the click happened /View v:The view that was clicked within the ListView /int position:The position of the view in the list /long id:The row id of the item that was clicked
@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); // Callback function when an item in the list is clicked // ListView l: The ListView where the click happened // View v: The view that was clicked within the ListView // int position: The position of the view in the list // long id: The row id of the item that was clicked } User Interaction in List View • User setListAdapter() to inflate the list view using the adapter • If the user select in the list a list entry the method onListItemClick() will be called. This method allows to access the selected element. 7
"4110词 Android iPhone B副ac0ermy public class MainActivity extends ListActivity Ubuntu public void onCreate(Bundle savedInstanceState){ Windows7 Max O5 X super.onCreate(savedInstanceState); String[]values=new String[]{"Android","iPhone","Windows Mobile", "Blackberry","WebOS","Ubuntu","Windows 7","Max OS X", "Linux","Windows 8"}; ArrayAdapteradapter new ArrayAdapter(this, android.R.layout.simple list item 1,R.id.label,values); setListAdapter (adapter); @Override protected void onListItemClick(ListView 1,View v,int position,long id){ super.onListItemclick(1,v,position,id); String item=(String)getListAdapter().getItem(position); Toast.makeText(this,item +selected with position"+position +and id"+id, Toast.LENGTH LONG)show () 8
8 public class MainActivity extends ListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] values = new String[] { "Android", "iPhone", "Windows Mobile", "Blackberry", "WebOS", "Ubuntu", "Windows 7", "Max OS X", "Linux", "Windows 8" }; ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, R.id.label, values); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); String item = (String) getListAdapter().getItem(position); Toast.makeText(this, item + " selected with position " + position + " and id " + id, Toast.LENGTH_LONG).show(); } }
Apply Your Own Layout 811:08 List View ■ListDisplay Android This is Tom's list view iPhone Android WindowsMobile iPhone WindowsMobile Blackberry Blackberry Webos Webos Ubuntu Ubuntu Windows7 Windows7 Max OS X Max0S× Using default layout in Using your own ListActivity layout file 9
Apply Your Own Layout Using default layout in ListActivity 9 Using your own layout file
Customize List View "@android:id/list"which is the In activity java file,still use the method setContentview() protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentview (R.layout.activity main); 10
Customize List View • In case you don't want to use one of the pre-defined layouts your own layout must have an element with the id "@android:id/list" which is the ListView • In activity java file, still use the method setContentView() 10 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }