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

The Archive Base Latest Questions

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

Let say I have my view that I use it as a toggle button.

  • 0

Let say I have my view that I use it as a toggle button. When user clicks it, I change the background via setBackgroundResource(). The number of list is around 15 items and ListView can show only around 7 items on screen.

At first, I try to use ListView.getChildAt(position) but when position is more than 7 it returns NullPointer. eventhough ListView.getCount() returns 15. But that’s make sense because it show only visible child.

Then I solve it by loop through all Data that binds to this Adapter, change the boolean value, and call notifyDataSetChange()

So the number of loop will be 15 for update data + 7 show visible view.

The best way should be 15 and that’s done.
Is there anyway to achieve this?

Thank

  • 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-16T09:07:14+00:00Added an answer on June 16, 2026 at 9:07 am

    Forget your child index. You should just toggle some type of flag in your adapter.

    Then when your getView method is called again it will redraw your list.

    i.e.:

    public class YourAdapter extends BaseAdapter {
    
       private boolean useBackgroundTwo = false;
    
       .. constructor ..
    
       @Override
       public View getView (int position, View convertView, ViewGroup parent) {
    
           ...
    
    
           ...
    
    
           View background = findViewById(...);
    
           int backgroundResource = R.drawable.one;
           if(useBackgroundTwo){
                backgroundResource = R.drawable.two;
           }
           background.setBackgroundResource(backgroundResource);
    
    
    
           ....
       }
    
        public void useNewBackground(){
           this.useBackgroundTwo = true;
           notifyDataSetChanged();
        }
    
        public void useOldBackground(){
           this.useBackgroundTwo = false;
           notifyDataSetChanged();
        }
    
    }
    

    Then in your activity code:

    ((YourAdapter) listview.getAdapter()).useNewBackground();
    

    Taking it further, you could use an enum instead of a boolean and have multiple methods setBackgroundGreen(), setBackgroundRed() or you could pass in the drawable you want to use setItemBackground(R.drawable.one); The choice is yours.

    Api: Adapter

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

Sidebar

Related Questions

So let's say I have a view that access a date: <%= Html.TextBoxFor(model =>
Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5
Let's say i have 2 files located in the same folder. /Test/View.cshtml <h1>File that
Let's say I have a page view called PagePreview.aspx . on that page I've
Let's say I have a ViewController that needs access to a view. In the
Let's say I have a ColorsController and a Colors/Index.cshtml View for that controller. I
Let's say we have an application that has a layered architecture. On the view
let's say I have a view where I currently only use Model information from
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Let's say I have 5 revisions of a README file. How do I view

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.