1

I'm trying to get a contextual menu using the Action Mode where I can select an item in a ListView by long-clicking on it. I created a MultiChoiceListener according to this reference and made a ItemLongClickListener wich sets an item as checked which is needed for the Action Mode to work according to this reference.

My problem is, that, even if the item long clicked animation is playing, the ActionMenu doesn't inflate.

ListView Code:

final ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setAdapter(adapter);
        listView.setLongClickable(true);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {

            Cursor cursor = db.getSubject(id);
            String subject = null;
            try {
                subject = cursor.getString(cursor.getColumnIndex("subject"));
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Intent intent = new Intent(Main.this, Marks.class);
            intent.putExtra("selected", subject);
            startActivity(intent);
            }
        });
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                listView.setItemChecked(arg2, true);
                return true;
            }

        });

        listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

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

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

            }

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

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position,
                    long id, boolean checked) {
                // TODO Auto-generated method stub

            }
        });

Thanks in advance!

Community
  • 1
  • 1
NiPfi
  • 1,710
  • 3
  • 18
  • 28

2 Answers2

10

I do not see any code in there to actually start the action mode. That is something your OnItemLongClickListener needs to do, such as:

  @Override
  public boolean onItemLongClick(AdapterView<?> view, View row,
                                 int position, long id) {
    modeView.clearChoices();
    modeView.setItemChecked(position, true);

    if (activeMode == null) {
      activeMode=host.startActionMode(this);
    }

    return(true);
  }

A full sample project demonstrating manually using an action mode like this can be found at: https://github.com/commonsguy/cw-omnibus/tree/master/ActionMode/Manual

Note that this project demonstrates using action modes via ActionBarSherlock.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • There's no seperate class handling the ActionMode in Googles example. Do I need to create a seperate class to use it? – NiPfi Oct 17 '12 at 15:12
  • @BlueHazard: That's up to you. You need something that implements `ActionMode.Callback` for use with `startActionMode()`. I elected to delegate that to a separate class. If you want to have your activity implement `ActionMode.Callback`, that should be perfectly fine. – CommonsWare Oct 17 '12 at 15:29
  • why not implement `MultiChoiceModeListener` and use `onCreateActionMode` ? – nurettin Nov 30 '16 at 21:10
  • @nurettin: The OP does. That is insufficient. – CommonsWare Nov 30 '16 at 21:12
1

CommonsWare's answer is great. If what you want is a MultiChoiceModeListener that selects one element by long-clicking it, this code might help you:

public class MyFragment extends Fragment implements MultiChoiceModeListener{
    private ListView mListView;
    // ...

    public void onViewCreated(View view, Bundle savedInstanceState) {
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        mListView.setMultiChoiceModeListener(this);

        mListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {                
                mListView.setItemChecked(arg2, true);
            }
        });
    }

In my case i do it with single click, but you can change OnItemClickListener for OnItemLongClickListener or any other that you prefer.

voghDev
  • 5,641
  • 2
  • 37
  • 41