Search code examples
androidandroid-fragmentsbaseadapterandroid-listfragment

Android : MultiChoiceModeListener not invoked inside ListFragment


For an ICS based application, I have created a ListFragment, which in turn uses a BaseAdapter implementation. I have enabled MultiChoiceModeListener() in order to display the Contextual Action Bar. But the issue here is that whenever I check the CheckBox or long press the Label(Both are in the View set in BaseAdapter), MultiChoiceModeListener implementation is not invoked at all. Any sort of help is much appreciated as I am completely stuck after trying many options!!!

public class ActivitiesFragment extends ListFragment {

public void onActivityCreated(Bundle savedInstanceState) {
    Log.d(TAG, "Entering onActivityCreated()");
    super.onActivityCreated(savedInstanceState);

    this.setAdapter();
    this.setHasOptionsMenu(true);
}

private void setAdapter() {

    HashMap<String, String> activities = DBAdapter
            .getInstance(this.context).getActivities();
    setListAdapter(new ActivitiesList(Util.sortByComparator(activities)));

    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new MultiSelectionListener());

}

private class ActivitiesList extends BaseAdapter {
    // Other functions declared
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ActivityView view = null;
        String activityName = this.activityList.get(position);
        String colour = this.activities.get(activityName);

        if (convertView == null) {
            // ActivityView is a LinearLayout with CheckBox, Label and a Button
            view = new ActivityView(context, activityName, colour);

        } else {
            view = (ActivityView) convertView;
            view.setActivityName(activityName);
        }
        return view;
    }
}

private class MultiSelectionListener implements MultiChoiceModeListener {
    // implementation
}

}

Solution

  • You need to use an ActionMode.Callback. You should read through these docs, it's actually pretty simple to use.