Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9147509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:00:23+00:00 2026-06-17T11:00:23+00:00

I’ve set up an ActionMode callback for use as the contextual ActionBar (CAB) within

  • 0

I’ve set up an ActionMode callback for use as the contextual ActionBar (CAB) within an ActionBarSherlock using project.

I’m trying to set up multiple select so that I can delete multiple items in a ListView.

I noticed while debugging, when the contextual ActionBar (CAB) is not open and I call isItemChecked() on a list item that I touch, it returns false as it should. But when the CAB IS open, items that I touch (that I haven’t touched before) return true on calls to isItemChecked(). Then when I call delete on the array from getCheckedItemIds(), that array does not contain the items that were previously returning true for isItemChecked().

Has anyone seen this?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.habit);
    habitListView = (ListView)findViewById(R.id.habitsListView);
    habitListView.setAdapter(mAdapter);
    habitListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    habitListView.setItemsCanFocus(false);
    habitListView.setOnItemLongClickListener(new MyOnItemLongClickListener());
    habitListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (habitListView.isItemChecked(position)) { // for debugging, returns false here
                                                         // when CAB isnt up.
                int h = 2;
            }
            // handle differently when CAB is on.
            if (mMode != null) {
                if (habitListView.isItemChecked(position)) { // returns true here when CAB is up
                                                             // but this is the first time I'm
                                                             // clicking the item
                    habitListView.setItemChecked(position, false);
                    // turn CAB off if this is the last to be unchecked
                    if (habitListView.getCheckedItemIds().length == 0) {
                        mMode.finish();
                    }
                } else {
                    habitListView.setItemChecked(position, true);
                }
            } else {
                // start detail/edit view activity
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getSupportMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.menu_create:
        Habit test = new Habit("FLOSS", "GOOD", "", "");
        mDbHelper.createHabitEntry(test);
        mAdapter.changeCursor(mDbHelper.getAllEntries());
        break;
    }
    return false;
}

private class MyOnItemLongClickListener implements OnItemLongClickListener {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        habitListView.setItemChecked(position, true);
        mMode = startActionMode(new MyActionModeCallback());
        return true;
    }
}

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

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        habitListView.setOnItemLongClickListener(new MyOnItemLongClickListener());
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.getMenuInflater().inflate(R.menu.main_long_click_context, menu);
        habitListView.setOnItemLongClickListener(null);
        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_delete:
            long[] selected = habitListView.getCheckedItemIds();
            if (selected.length > 0) {
                for (long id : selected) {
                    mDbHelper.deleteEntry(id);
                }
            }
            mAdapter.changeCursor(mDbHelper.getAllEntries());
            mode.finish();
            break;
        }
        return true;
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:00:24+00:00Added an answer on June 17, 2026 at 11:00 am

    So what has worked for me is to just use

       habitListView.setOnItemClickListener(new OnItemClickListener(){
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
    
                //handle differently when CAB is on.
                if (mMode != null){
    
                    view.setSelected(!view.isSelected());
    
                    updateCABtitle();
    
                    //turn CAB off if this is the last to be unchecked
                        if (habitListView.getCheckedItemIds().length == 0){
                            mMode.finish();
                        } 
    
                } else {
                    //start detail/edit view activity
                    Intent intent = new Intent(getApplicationContext(), HabitDetailActivity.class);
                    startActivity(intent);
    
                }
    
            }
    
        });
    

    and

        private class MyOnItemLongClickListener implements OnItemLongClickListener{
    
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                        int position, long id) {
    
    
                habitListView.setItemChecked(position, true);
    
                mMode = startActionMode(new MyActionModeCallback());
                updateCABtitle();
    
                return true;
        }
    
    }
    

    I don’t really understand why setItemChecked() is not working for onItemClick() but seems to be for onItemLongClick(). It looks as though there is a default click handler that gets called in AbsListView.PerformItemClick() that must do some toggling of checked/unchecked or something.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.