1111import android .widget .AdapterView .OnItemLongClickListener ;
1212import android .widget .Toast ;
1313
14- public class MyListActivityActionbar extends ListActivity {
14+ public class MyListActivityActionbar extends ListActivity
15+ implements ActionMode .Callback {
1516
1617 protected Object mActionMode ;
1718 public int selectedItem = -1 ;
1819
20+ @ Override
1921 public void onCreate (Bundle icicle ) {
2022 super .onCreate (icicle );
21- String [] values = new String [] { "Android" , "iPhone" , "WindowsMobile" ,
22- "Blackberry" , "WebOS" , "Ubuntu" , "Windows7" , "Max OS X" ,
23- "Linux" , "OS/2" , "Ubuntu" , "Windows7" , "Max OS X" , "Linux" ,
24- "OS/2" , "Ubuntu" , "Windows7" , "Max OS X" , "Linux" , "OS/2" };
23+ String [] values = new String [] { "Android" ,
24+ "iPhone" , "WindowsMobile" ,
25+ "Blackberry" , "WebOS" , "Ubuntu" ,
26+ "Windows7" , "Max OS X" , "Linux" , "OS/2" ,
27+ "Ubuntu" , "Windows7" , "Max OS X" ,
28+ "Linux" , "OS/2" , "Ubuntu" ,
29+ "Windows7" , "Max OS X" ,
30+ "Linux" , "OS/2" };
2531
2632 MySimpleArrayAdapter adapter = new MySimpleArrayAdapter (this , values );
2733 setListAdapter (adapter );
2834
2935 getListView ().setOnItemLongClickListener (new OnItemLongClickListener () {
3036
3137 @ Override
32- public boolean onItemLongClick (AdapterView <?> parent , View view ,
33- int position , long id ) {
38+ public boolean onItemLongClick (AdapterView <?> parent , View view , int position , long id ) {
3439
3540 if (mActionMode != null ) {
3641 return false ;
3742 }
3843 selectedItem = position ;
3944
4045 // Start the CAB using the ActionMode.Callback defined above
41- MyListActivityActionbar .this
42- .startActionMode (mActionModeCallback );
46+ MyListActivityActionbar .this .startActionMode (MyListActivityActionbar .this );
4347 view .setSelected (true );
4448 return true ;
4549 }
4650 });
4751 }
4852
49- private ActionMode .Callback mActionModeCallback = new ActionMode .Callback () {
50-
51- // Called when the action mode is created; startActionMode() was called
52- public boolean onCreateActionMode (ActionMode mode , Menu menu ) {
53- // Inflate a menu resource providing context menu items
54- MenuInflater inflater = mode .getMenuInflater ();
55- // Assumes that you have "contexual.xml" menu resources
56- inflater .inflate (R .menu .rowselection , menu );
57- return true ;
58- }
53+ private void show () {
54+ Toast .makeText (MyListActivityActionbar .this , String .valueOf (selectedItem ), Toast .LENGTH_LONG ).show ();
55+ }
5956
60- // Called each time the action mode is shown. Always called after
61- // onCreateActionMode, but
62- // may be called multiple times if the mode is invalidated.
63- public boolean onPrepareActionMode (ActionMode mode , Menu menu ) {
64- return false ; // Return false if nothing is done
65- }
57+ // Called when the action mode is created; startActionMode() was called
58+ @ Override
59+ public boolean onCreateActionMode (ActionMode mode , Menu menu ) {
60+ // Inflate a menu resource providing context menu items
61+ MenuInflater inflater = mode .getMenuInflater ();
62+ // Assumes that you have "contexual.xml" menu resources
63+ inflater .inflate (R .menu .rowselection , menu );
64+ return true ;
65+ }
6666
67- // Called when the user selects a contextual menu item
68- public boolean onActionItemClicked (ActionMode mode , MenuItem item ) {
69- switch (item .getItemId ()) {
70- case R .id .menuitem1_show :
71- show ();
72- // Action picked, so close the CAB
73- mode .finish ();
74- return true ;
75- default :
76- return false ;
77- }
78- }
67+ // Called each time the action mode is shown. Always called after
68+ // onCreateActionMode, but
69+ // may be called multiple times if the mode is invalidated.
70+ @ Override
71+ public boolean onPrepareActionMode (ActionMode mode , Menu menu ) {
72+ return false ; // Return false if nothing is done
73+ }
7974
80- // Called when the user exits the action mode
81- public void onDestroyActionMode (ActionMode mode ) {
82- mActionMode = null ;
83- selectedItem = -1 ;
75+ // Called when the user selects a contextual menu item
76+ @ Override
77+ public boolean onActionItemClicked (ActionMode mode , MenuItem item ) {
78+ switch (item .getItemId ()) {
79+ case R .id .menuitem1_show :
80+ show ();
81+ // Action picked, so close the CAB
82+ mode .finish ();
83+ return true ;
84+ default :
85+ return false ;
8486 }
85- };
87+ }
8688
87- private void show () {
88- Toast .makeText (MyListActivityActionbar .this ,
89- String .valueOf (selectedItem ), Toast .LENGTH_LONG ).show ();
89+ // Called when the user exits the action mode
90+ @ Override
91+ public void onDestroyActionMode (ActionMode mode ) {
92+ mActionMode = null ;
93+ selectedItem = -1 ;
9094 }
9195
9296}
0 commit comments