MultiChoiceModeListener() problem


#1

Hi All

Eclipse is telling me that MultiChoiceModeListener() must implement the inherited abstract method ActionMode.Callback.onActionItemClicked(ActionMode, MenuItem)

Thing is, it IS implementing that method as with all the other methods. Here’s the code:

[code]listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

			public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
				//not used
			}
			
			//ActionMode.Callback methods
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				MenuInflater inflater = mode.getMenuInflater();
				inflater.inflate(R.menu.crime_list_item_context, menu);
				return true;
			}
			
			public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
				return false;
				//not used
			}
			
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				switch (item.getItemId()) {
				case R.id.menu_item_delete_crime:
					CrimeAdapter adapter = (CrimeAdapter)getListAdapter();
					CrimeLab crimeLab = CrimeLab.get(getActivity());
					for (int i = adapter.getCount() - 1; i>=0; i--) {
						if (getListView().isItemChecked(i)) {
							crimeLab.deleteCrime(adapter.getItem(i));
						}
					}
					mode.finish();
					adapter.notifyDataSetChanged();
					return true;
				default:
					return false;
				}
			}
			
			public void onDestroyActionMode(ActionMode mode){
				//nothing required here
			}
			
		});[/code]

Have tried cleaning the project/restarting eclipse. can anyone see the prob??


#2

I took a quick look for typos but didn’t see any. I think it’s easiest if you let Eclipse auto-complete the stubs for you. I suspect you didn’t do it that way since the override annotations aren’t there.

Here is my code if you want to try copying and pasting to rule out a typo we’ve overlooked.

[code] listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
// Not used
}

			@Override
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				MenuInflater inflater = mode.getMenuInflater();
				inflater.inflate(R.menu.crime_list_item_context, menu);
				return true;
			}
			
			@Override
			public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
				return false;
			}
			
			@Override
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				switch (item.getItemId()) {
					case R.id.menu_item_delete_crime:
						CrimeAdapter adapter = (CrimeAdapter)getListAdapter();
						CrimeLab crimeLab = CrimeLab.get(getActivity());
						for (int i = adapter.getCount() - 1; i >= 0; i--) {
							if (getListView().isItemChecked(i)) {
								crimeLab.deleteCrime(adapter.getItem(i));
							}
						}
						crimeLab.saveCrimes();
						mode.finish();
						adapter.notifyDataSetChanged();
						return true;
				}
				return false;
			}
			
			@Override
			public void onDestroyActionMode(ActionMode mode) {
				// Not used.
			}
		});

[/code]


#3

Hmm if I add the @Override annotations I get the same error:

The type new AbsListView.MultiChoiceModeListener(){} must implement the inherited abstract method ActionMode.Callback.onActionItemClicked(ActionMode, MenuItem)

and also on each method:

The method onCreateActionMode(ActionMode, Menu) of type new AbsListView.MultiChoiceModeListener(){} must override or implement a supertype method

Am I importing the right class? Was the only one eclipse offered: android.widget.AbsListView.MultiChoiceModeListener;

I’ll try cutting/pasting your code. - nah same issue. Also the book doesn’t include any @Override annotations?


#4

@Override is never required and yes, the book sometimes omits them. But if you let Eclipse make the stubs, it will add the @Overrides for you. As you know, that won’t fix your problem, but it doesn’t hurt anything either. Imagine if the Interface changed in the future for example.

Anyway, yes, you are importing the correct package. Sorry, I couldn’t help. Try using downloading the entire CrimeListFragment.java that the authors published and using it. If it works, diff it with yours.

You can also try using the correct indentation feature of Eclipse in case it’s something stupid simple like indentation faking you out.


#5

I’ve fixed this, by using android.view.ActionMode instead of ActionMode to cast the first arguments in the methods. Very strange.


#6

Using Android Studio to go through the book. In my version, it did not import correctly. Added import android.widget.AbsListView.MultiChoiceModeListener; and it worked correctly. Before it would add the AbsListView. prefix to MultiChoiceModeListener and I got nothing but more errors. Hope this helps someone in the future.