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

  • Home
  • SEARCH
  • 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 9141247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:37:13+00:00 2026-06-17T09:37:13+00:00

I have a ListView in which every second item should have a different color

  • 0

I have a ListView in which every second item should have a different color (for example: white -> gray -> white ->gray etc.), it seems to work initially but as soon as I start scrolling, the rendering of the backgroundcolor seems to fail: the other color is set randomly to the items, also when I scroll up again this happens to the items where the color was correct at the beginning. Can someone hint me why this is happening?

I have tried this in the Adapter:

    if((position % 2) == 1) {
        layoutHolder.setBackgroundColor(getContext().getResources().getColor(R.color.light_green));
    }

and this in my ListFragment:

    private AsyncTask<Void, Void, Void> fillList = new AsyncTask<Void, Void, Void>() {

    @Override
    protected Void doInBackground(Void... params) {
        addListItems();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        for (int i = 1; i < getListView().getChildCount()-1; i = i+2) {
            LOGI(TAG, "for-loop " + String.valueOf(i));
            getListView().getChildAt(i).setBackgroundColor(getSherlockActivity().getResources().getColor(R.color.light_green));
        }
    }   
};

UPDATE:

I have done a bit of logging and found out that, if I am adding more elements than visible on the screen and check for the ListViews child count, the ListView only holds the children which are initially visible to the screen, the rest seems to be loaded/added when scrolling is done.

  • 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-17T09:37:13+00:00Added an answer on June 17, 2026 at 9:37 am

    in your adapter use this code.. its work.. u need to provide else condition.

    if ( position % 2 == 0 ){
                    convertView.setBackgroundColor(Color.GREEN);
                }else{
                    convertView.setBackgroundColor(Color.RED);
                }
    

    enter image description here

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

Sidebar

Related Questions

I have a ListView in which I inflate 4 different layouts dynamically. In every
This is my ListView xml file and i have different xml for row(every item
I have a Layout which as a ImageView, ListView, some TextViews etc wrapped in
I have list view. In which, every list item is a document(.pdf, .xls ,
I have a ListView which displays in every row an image pulled from the
I have a ListView that displays every item in an array called, Facts_Array. What
I have an app which mains view is a listview. Each item consist of
I have ListView which is saving all data to database. For adding i have
I have a ListView which displays a list of string values. I want to
I have a ListView which might contains a lot of items, so it is

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.