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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:12:19+00:00 2026-05-27T23:12:19+00:00

basically, I have a ListView in my android application. I want it to be

  • 0

basically, I have a ListView in my android application. I want it to be possible for the user to select multiple rows by clicking on them. To implement this, I’ve made a class that implements onItemClickListener, and the ListView sets this as the OnItemClick Listener.

This is the code in the listener:

public void onItemClick(AdapterView<?> parentAdapterView, View viewSelected, int pos, long id) {
    if(selectedItems.contains(viewSelected)) {
        //remove it from the selected list.
        selectedItems.remove(viewSelected);
        viewSelected.setBackgroundColor(Color.BLACK);
    }
    else {
        selectedItems.add(viewSelected);
        viewSelected.setBackgroundColor(Color.RED);
    }
}

The selectedItems is simply a List of Views (List), that I used to keep track of all the items that have been selected.

This works fine until the number of items causes the list to overflow (and thus, the list becomes scrollable). Then, when one item is clicked, another item is highlighted (in addition to the first) further down the list?

Can’t think why this would be happening? I’ve searched around on Google, but to no avail…

I’d be grateful for any help on the matter.

Cheers

Edit: The code that I used to provide the views for list view is simply:

uiListViewRes = R.layout.main_list_item;
ListView overTwoDaysView = (ListView) findViewById(R.id.overtwolistview);

String[] from = {"_id","foodItemName", "expire", "dateAdded"};
int[] to = {R.id.itemIDhidden,R.id.name, R.id.expiry, R.id.dateAddedLabel};

SimpleAdapter overTwoDaysAdapter = new SimpleAdapter(this, adapter.getAllItemsOverTwoDays(), uiListViewRes, from, to);

overTwoDaysView.setAdapter(overTwoDaysAdapter);
  • 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-05-27T23:12:19+00:00Added an answer on May 27, 2026 at 11:12 pm

    The problem is that ListView reuses Views for better performance.

    So ListView allocates only X number of Views and then tries to reuse the not visible ones – by changing the properties of the View.

    That means that you can’t save the Views/set the background of the Views because that same View instance will be used later in the ListView.


    There are several approaches to solve this problem:

    1. You can have a look at the ListView’s CHOICE_MODE_MULTIPLE

      listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
      

      Example: http://www.vogella.de/articles/AndroidListView/article.html

    2. You can save the ID or position of the row and use it in a custom Adapter to set the background accordingly.

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

Sidebar

Related Questions

I wish to create a tab application in Android. I wish to basically have
I have a WPF app with a listview control. The application basically loads a
Is it possible to have a watermark background for a listview control in Android?
So i basically have this ListView and i would like to press Tab and
I want to have a Listview adapter where each rows uses it's own convertview,
I basically have something like this: void Foo(Type ty) { var result = serializer.Deserialize<ty>(inputContent);
I basically have three tables, posts, images and postimages (this simply contains the ids
I basically have 7 select statements that I need to have the results output
I have a listview say in this instance has the values: UK, USA, FRANCE
ASP.NET application, VB or C# doesn't matter. I have Listview which databound to Dataset

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.