/*** Copyright (c) 2008-2012 CommonsWare, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.commonsware.android.actionmode; import android.app.ListActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; public class ActionModeDemo extends ListActivity { private static final String[] items= { "lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat", "ante", "porttitor", "sodales", "pellentesque", "augue", "purus" }; private ArrayList<String> words=null; private ArrayAdapter<String> adapter=null; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); initAdapter(); getListView().setLongClickable(true); getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); getListView().setOnItemLongClickListener(new ActionModeHelper( this, getListView())); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.actions, menu); return(super.onCreateOptionsMenu(menu)); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add: addWord(); return(true); case R.id.reset: initAdapter(); return(true); case R.id.about: Toast.makeText(this, R.string.about_toast, Toast.LENGTH_LONG) .show(); return(true); } return(super.onOptionsItemSelected(item)); } public boolean performAction(int itemId, int position) { switch (itemId) { case R.id.cap: String word=words.get(position); word=word.toUpperCase(); adapter.remove(words.get(position)); adapter.insert(word, position); return(true); case R.id.remove: adapter.remove(words.get(position)); return(true); } return(false); } private void initAdapter() { words=new ArrayList<String>(); for (int i=0;i<5;i++) { words.add(items[i]); } adapter=new ArrayAdapter<String>( this, android.R.layout.simple_list_item_activated_1, words); setListAdapter(adapter); } private void addWord() { if (adapter.getCount()<items.length) { adapter.add(items[adapter.getCount()]); } } }