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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:13:40+00:00 2026-05-22T18:13:40+00:00

I know how to access a listview item through an onitemclicklistener but how do

  • 0

I know how to access a listview item through an onitemclicklistener but how do I do something like change the background color in code.

sudo code:

lv[0].setBackgroundResource(R.color.red); //change the background of the first listview item

Is there a way to access each view of the listview?

  • 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-22T18:13:41+00:00Added an answer on May 22, 2026 at 6:13 pm

    @Matt: It’s not that it’s weak, you just have to understand the way it works.

    Yellavon, at any given time, your ListView only contains the visible items, so you can’t access and change items directly. As they scroll out of view, the views are recycled, and populated with data by the ListAdapter. It’s within this adapter that you need to handle the cases where items should differ. If you’ve created your own custom ListAdapter (e.g. ArrayAdapter, BaseAdapter), in your getView() method, simply add some logic within to handle the cases in which the background color should change.

    Let’s say for example, you have a list of integers, and you want any integer >= 50 to show up in red:

    if(items.get(position) >= 50) {
        myView.setBackgroundColor("#FF0000");
    } else {
        myView.setBackgroundColor("#000000");
    }
    

    (It’s important to make sure to handle the else cases, as you may get a recycled view of an item that had been colored red. In this case, you’d have to reset it to whatever default background color you need.)

    If you’ve never built a custom adapter, this excerpt from CommonsWare’s book on creating custom ListAdapters is a great resource.

    EDIT: Further thought, based on your comment:

    In your custom ExpandableListAdapter

    private Object lastSelectedObject;
    
    public void setLastSelectedObject(Object obj) {
        lastSelectedObject = obj;
    }
    
    public Object getLastSelectedObject() {
        return lastSelectedObject;
    }
    

    Implement onListItemClick(ListView l, View v, int pos, long id) in your ListActivity

    @Override
    protected void onListItemClick(ListView l, View v, int pos, long id) {
        super.onListItemClick(l, v, pos, id);
        ((CustomAdapter)l.getAdapter()).setLastSelectedObject(items.get(pos));
    }
    

    Now, back in getView()

    Object obj = getLastSelectedObject();
    if(obj != null) {
        //handle background switching for your View here
    } else {
        //reset background to default for recycled views
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We all know doing something like this is bad: <ul> <li>Item</li> <li>Item</li> ... 500
In WPF, how do I access the items of a ListView? I know SelectedValuePath=...
I want to do something like mylistview.setElementsofView(0).getElementById.setColor(black); currently the only way I know of
I know to access scripts from another module, you just pass the module name
As you know anyone can access strings in an native application using a hex
I just want to know how to access an array in SMARTY that have
As far as I know I can access the web.xml <context-param> s by making
Can anyone let me know how can access an element of a list that
By any chance, does anyone know how to access the phone's photo gallery? I
Know if it's possible to access the iPhone compass in Safari using JavaScript? I

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.