ActionBarSherlock Even more advanced integration


#1

So, I’m working through all the challenges and this one is throwing me for a loop.

I’m able to select multiple items on long press and the context menu comes up like it should, but
[ul]1.The background color doesn’t change, might not even select it.
2.The delete button isn’t working.[/ul]

Following is the code that is within my onCreateView function in CrimeListFragment.java

[code]@TargetApi(11)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = emptyView(inflater, parent, savedInstanceState);

	ListView listView = (ListView)v.findViewById(android.R.id.list);

	// Use contextual action bar on honeycomb and higher
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
		if (mSubtitleVisible) { 
			//getActivity().getActionBar().setSubtitle(R.string.subtitle);
			getSherlockActivity().getSupportActionBar().setSubtitle(R.string.subtitle);
		}

		//listView.setLongClickable(true);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				getSherlockActivity().startActionMode(new com.actionbarsherlock.view.ActionMode.Callback() {

					@Override
					public boolean onPrepareActionMode(
							com.actionbarsherlock.view.ActionMode mode,
							com.actionbarsherlock.view.Menu menu) {
						// TODO Auto-generated method stub
						return false;
					}

					@Override
					public void onDestroyActionMode(com.actionbarsherlock.view.ActionMode mode) {
						// TODO Auto-generated method stub

					}

					@Override
					public boolean onCreateActionMode(
							com.actionbarsherlock.view.ActionMode mode,
							com.actionbarsherlock.view.Menu menu) {
						com.actionbarsherlock.view.MenuInflater inflater = mode.getMenuInflater();
						inflater.inflate(R.menu.crime_list_item_context, menu);
						return true;
					}

					@Override
					public boolean onActionItemClicked(
							com.actionbarsherlock.view.ActionMode mode,
							com.actionbarsherlock.view.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;
						}
					}
				});

				return true;
			}
		});
	} else { // Use floating text menus on froyo and gingerbread
		registerForContextMenu(listView);
	}	

	return v;
}[/code]

I’m not sure where I’m going wrong. I’m not sure if I need to keep the Version check there or not as well, since ActionBarSherlock should make all versions of Android uniform with the context menu.

Any help would be appreciated. Thanks


#2

So what happens when you tap the delete button? Nothing at all, or does it dismiss the action mode without doing anything?

The #1 thing that jumps out at me that I don’t see is code to toggle the selection mode of the ListView. With CHOICE_MODE_MULTIPLE_MODAL, ListView does this for you. But without that, it is necessary to manually switch between CHOICE_MODE_NONE and CHOICE_MODE_MULTIPLE as your action mode is shown and hidden.


#3

It dismisses the action mode without doing anything at all. It’ll select as many as I want, if I remember correctly (moved on from this challenge), and then whenever I click on the delete button, it just dismisses and all the previously “selected” are still there.

I do see what you’re saying about the CHOICE_MODE_MULTIPLE though. I just read back through the challenge and saw that it said that that flag worked on all versions of Android. That could most definitely been the thing that was causing me issues. I’ll have to try that when I get the chance to see if that was the actual case.