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 8296747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:08:20+00:00 2026-06-08T15:08:20+00:00

I’m currently using a ListView to display a sort of items. I’ve implemented an

  • 0

I’m currently using a ListView to display a sort of items. I’ve implemented an action mode to select multiple items and to delete massively that works well in android 4.x. But when I tried with API version 8 or 9 (android 2.2.x/2.3.x), selection works internally as expected but row items are colored randomly.

If user selects first row, internally first row is selected, but row number 4 is colored. When I click another row, this row and first one is colored. It’s a strange behaviour which I expect to work normally as on 4.x devices.

Long-click overriding to activate action mode and check long-clicked item of ListView:

@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
    if (actionMode == null) {
        listView.setOnItemClickListener(new CABClickListener());
        actionMode = startActionMode(new ListActionMode());
        // Check item pressed with long click
        listView.setItemChecked(position, true);
        view.setBackgroundColor(checkedColor);
        logger.debug("Item at pos. " + position + ", checked.");
    }
    return true;
}

CABClickListener, responsible to check/uncheck items of ListView, marking them internally and changing its background color:

private final class CABClickListener implements AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (listView.isItemChecked(position)) {
            view.setBackgroundColor(checkedColor);
            logger.debug("Item at pos. " + position + ", checked.");
        } else {
            view.setBackgroundColor(uncheckedColor);
            logger.debug("Item at pos. " + position + ", unchecked.");
        }
    }
}

Those classes/methods are inside the Activity, listView is declared in top of it.

More considerations:

  • Using ActionBarSherlock (it shows the CAB but I think this is not
    important here) and Roboguice, but I haven’t any problem with that.
  • I was always developing with the emulator. In addition, I couldn’t try my app with android 3.x (got problems with this version, emulator doesn’t launch), so I don’t know if the problem persists in these versions. UPDATE: Tested in android 3.0 API 11, works well as on 4.x.
  • I debugged the code and Views in both methods are ok, but when I call
    view.setBackgroundColor(checkedColor);, another View is colored.

Any suggestion? Hope anyone can help!

  • 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-08T15:08:22+00:00Added an answer on June 8, 2026 at 3:08 pm

    Wow, the problem was in my own implementation of ArrayAdapter, where I tried to apply view holder pattern. I discarded this initially because I tested the same with a simple ArrayAdapter and problem was still there.

    The difference between android APIs is that when item is clicked in the 8-10 API, all list is repainted, reusing existing views. Therefore, when you click in a item (View), this is colored but immediatly android repaints all list, reusing the views, and making the colored one to be in other position. When a list view item gets clicked in >11 API, anything gets repainted (yes, great performance improvement between versions) and the correct item view was painted succesfully (calling properly view.setBackgroundColor(checkedColor)).

    Finally, I solved this strange behaviour, storing checked state in the entities. With this, when the view has to be recycled, checked value can be recovered and list item can be colored without problems.

    I post my GenericListAdapter.getView() method and related for anyone interested.

    GenericListAdapter<T>.getView():

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewHolder<T> viewHolder = null;
    
        if (convertView == null || !(convertView.getTag() instanceof ItemViewHolder<?>)) {
            logger.debug("New view: " + convertView + " at position: " + position);
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(resource, null);
    
            viewHolder = GenericViewHolderFactory.createInstance(clazz);
            viewHolder.setContext(context);
            viewHolder.saveViewContents(convertView);
    
            convertView.setTag(viewHolder);
        } else {
            logger.debug("Reusing view: " + convertView + ", at position: " + position);
            viewHolder = (ItemViewHolder<T>) convertView.getTag();
        }
    
        T entity = getItem(position);
        viewHolder.setViewFields(entity, convertView);
    
        return convertView;
    }
    

    And the ViewHolder implementation which refreshes the recycled view:

    public class EventItemViewHolder implements ItemViewHolder<Event> {
    
    ...
    
        @Override
        public void setViewFields(Event event, View convertView) {
            name.setText(event.getName());
            amount.setText(event.getTotalAmount().toString());
    
            if (event.isChecked()) {
                convertView.setBackgroundColor(checkedColor);
            } else {
                convertView.setBackgroundColor(uncheckedColor);
            }
        }
    }
    

    I hope I’ve explained myself well.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.