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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:01:03+00:00 2026-05-18T20:01:03+00:00

I have an application in which I’d like one row at a time to

  • 0

I have an application in which I’d like one row at a time to have a certain color. This seems to work about 95% of the time, but sometimes instead of having just one row with this color, it will allow multiple rows to have the color. Specifically, a row is set to have the “special” color when it is tapped. In rare instances, the last row tapped will retain the color despite a call to setBackgroundColor attempting to make it otherwise.

private OnItemClickListener mDirectoryListener = new OnItemClickListener(){
    public void onItemClick(AdapterView parent, View view, int pos, long id){
        if (stdir.getStationCount() == pos) {
            stdir.moreStations();  
            return;
        }

        if (playingView != null)
            playingView.setBackgroundColor(Color.DKGRAY);

        view.setBackgroundColor(Color.MAGENTA);
        playingView = view;
        playStation(pos);
    }
};

I have confirmed with print statements that the code setting the row to gray is always called.

Can anyone imagine a reason why this code might intermittently fail? If there is a pattern or condition that causes it, I can’t tell.

I thought it might have something to do with the activity lifecycle setting the “playingView” variable back to null, but I can’t reliably reproduce the problem by switching activities or locking the phone.

private class DirectoryAdapter extends ArrayAdapter {
    private ArrayList<Station> items;

    public DirectoryAdapter(Context c, int resLayoutId, ArrayList<Station> stations){
        super(c, resLayoutId, stations);            
        this.items = stations;
    }

    public int getCount(){
        return items.size() + 1;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        View v = convertView;
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (position == this.items.size()) {
            v = vi.inflate(R.layout.morerow, null);
            return v;
        }

        Station station = this.items.get(position);
        v = vi.inflate(R.layout.songrow, null);

        if (station.playing)
            v.setBackgroundColor(Color.MAGENTA);
        else if (station.visited)
            v.setBackgroundColor(Color.DKGRAY);
        else
            v.setBackgroundColor(Color.BLACK);

        TextView title  = (TextView)v.findViewById(R.id.title);
        title.setText(station.name);
        return v;
    }    
};
  • 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-18T20:01:04+00:00Added an answer on May 18, 2026 at 8:01 pm

    ListViews don’t create instances of contained views for every item in the list, but only for ones that are actually visible on the screen. For performance reasons, they try and maintain as few views as possible, and recycle them. That’s what the convertView parameter is.

    When a view scrolls off the screen, it may be recycled or destroyed. You can’t hold a reference to an old view and assume that it will refer to the item you expect it to in the future. You should save the ID of the list item you need and look that up instead.

    Moreover, there are a couple of other issues with your implementation (from a best practices standpoint). You seem to be ignoring the convertView parameter and creating a new view from scratch each time. That can cause your application to bog down a bit while scrolling if you have a long list. Secondly, instead of adding the “more” element the way you do, you’re better of setting it with setFooterView().

    There’s an excellent talk on the ListView from Google I/O 2010 that covers these and other issues. It’s an hour long, but definitely worth watching in its entirety.

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

Sidebar

Related Questions

I have an application which really should be installed, but does work fine when
I have one application which uses the standard .NET forms authentication cookie, now I
I have an application which serves more than one website. Similar to Stack Exchange,
I currently have a application which will display the time allocated to a user
I have an application which extracts data from an XML file using XPath. If
We have an application which needs to use Direct3D. Specifically, it needs at least
I have an application which behaves as a slideshow for all pictures in a
I have an application which is a portal application and I want to allow
I have an application which takes a string value of the form %programfiles%\directory\tool.exe from
I have an application which get copied and run on client machines. The program

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.