Home | History | Annotate | Download | only in view
      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  */
     33 public class List15 extends ListActivity {
     34     @Override
     35     protected void onCreate(Bundle savedInstanceState) {
     36         super.onCreate(savedInstanceState);
     37         ListView lv = getListView();
     38         lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
     39         lv.setMultiChoiceModeListener(new ModeCallback());
     40         setListAdapter(new ArrayAdapter<String>(this,
     41                 android.R.layout.simple_list_item_checked, mStrings));
     42     }
     43 
     44     @Override
     45     protected void onPostCreate(Bundle savedInstanceState) {
     46         super.onPostCreate(savedInstanceState);
     47         getActionBar().setSubtitle("Long press to start selection");
     48     }
     49 
     50     private class ModeCallback implements ListView.MultiChoiceModeListener {
     51 
     52         public boolean onCreateActionMode(ActionMode mode, Menu menu) {
     53             MenuInflater inflater = getMenuInflater();
     54             inflater.inflate(R.menu.list_select_menu, menu);
     55             mode.setTitle("Select Items");
     56             setSubtitle(mode);
     57             return true;
     58         }
     59 
     60         public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
     61             return true;
     62         }
     63 
     64         public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
     65             switch (item.getItemId()) {
     66             case R.id.share:
     67                 Toast.makeText(List15.this, "Shared " + getListView().getCheckedItemCount() +
     68                         " items", Toast.LENGTH_SHORT).show();
     69                 mode.finish();
     70                 break;
     71             default:
     72                 Toast.makeText(List15.this, "Clicked " + item.getTitle(),
     73                         Toast.LENGTH_SHORT).show();
     74                 break;
     75             }
     76             return true;
     77         }
     78 
     79         public void onDestroyActionMode(ActionMode mode) {
     80         }
     81 
     82         public void onItemCheckedStateChanged(ActionMode mode,
     83                 int position, long id, boolean checked) {
     84             setSubtitle(mode);
     85         }
     86 
     87         private void setSubtitle(ActionMode mode) {
     88             final int checkedCount = getListView().getCheckedItemCount();
     89             switch (checkedCount) {
     90                 case 0:
     91                     mode.setSubtitle(null);
     92                     break;
     93                 case 1:
     94                     mode.setSubtitle("One item selected");
     95                     break;
     96                 default:
     97                     mode.setSubtitle("" + checkedCount + " items selected");
     98                     break;
     99             }
    100         }
    101     }
    102 
    103     private String[] mStrings = Cheeses.sCheeseStrings;
    104 }
    105