1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.apis.view; 17 18 import com.example.android.apis.R; 19 20 import android.app.ListActivity; 21 import android.os.Bundle; 22 import android.view.ActionMode; 23 import android.view.Menu; 24 import android.view.MenuInflater; 25 import android.view.MenuItem; 26 import android.widget.ArrayAdapter; 27 import android.widget.ListView; 28 import android.widget.Toast; 29 30 /** 31 * This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on ListView 32 * couple with the new simple_list_item_activated_1 which uses a highlighted border for selected 33 * items. 34 */ 35 public class List16 extends ListActivity { 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 ListView lv = getListView(); 40 lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 41 lv.setMultiChoiceModeListener(new ModeCallback()); 42 setListAdapter(new ArrayAdapter<String>(this, 43 android.R.layout.simple_list_item_activated_1, Cheeses.sCheeseStrings)); 44 } 45 46 @Override 47 protected void onPostCreate(Bundle savedInstanceState) { 48 super.onPostCreate(savedInstanceState); 49 getActionBar().setSubtitle("Long press to start selection"); 50 } 51 52 private class ModeCallback implements ListView.MultiChoiceModeListener { 53 54 public boolean onCreateActionMode(ActionMode mode, Menu menu) { 55 MenuInflater inflater = getMenuInflater(); 56 inflater.inflate(R.menu.list_select_menu, menu); 57 mode.setTitle("Select Items"); 58 return true; 59 } 60 61 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 62 return true; 63 } 64 65 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 66 switch (item.getItemId()) { 67 case R.id.share: 68 Toast.makeText(List16.this, "Shared " + getListView().getCheckedItemCount() + 69 " items", Toast.LENGTH_SHORT).show(); 70 mode.finish(); 71 break; 72 default: 73 Toast.makeText(List16.this, "Clicked " + item.getTitle(), 74 Toast.LENGTH_SHORT).show(); 75 break; 76 } 77 return true; 78 } 79 80 public void onDestroyActionMode(ActionMode mode) { 81 } 82 83 public void onItemCheckedStateChanged(ActionMode mode, 84 int position, long id, boolean checked) { 85 final int checkedCount = getListView().getCheckedItemCount(); 86 switch (checkedCount) { 87 case 0: 88 mode.setSubtitle(null); 89 break; 90 case 1: 91 mode.setSubtitle("One item selected"); 92 break; 93 default: 94 mode.setSubtitle("" + checkedCount + " items selected"); 95 break; 96 } 97 } 98 99 } 100 } 101