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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:23:04+00:00 2026-06-03T20:23:04+00:00

I have a ListView in my Activity and for each row Im using my

  • 0

I have a ListView in my Activity and for each row Im using my custom layout with a TextView and two Buttons. When I click any of these two buttons, I want a certain action to be performed. In my ArrayAdapter, in getView method, I set onClickListeners to these two buttons.

public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;
    if (v == null) {
        v = li.inflate(R.layout.process_row, null);
    }


    final Button processCheck = (Button) v.findViewById(R.id.processCheck);
    processCheck.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (process.isChecked() == false) {
                process.setChecked(true);
                processCheck.setBackgroundColor(Color.BLUE);
            }
            else {
                process.setChecked(false);
                processCheck.setBackgroundColor(Color.RED);
            }

        }
    });

    return v;
}

ArrayList holds objects of mz custom Class Process and process in the code is an instance of this class. So now when I click this button in one of the ListView rows, I would expect to change the boolean variable in the given Process instance and change the color of the button. That happens, but not only with this one row, but some 3-4 more. So after clicking, I have some 5 changed buttons instead of one. Do you know what im doing wrong here? Note that I dont know the ListActivity coding very well, most of the code is actually copied and edited a little bit from an example file. Thanks!

EDIT:

The problem is probably only with the layout somewhere. When I click the button, more of them change color but only the Process where i clicked has its boolean value changed.

  • 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-03T20:23:07+00:00Added an answer on June 3, 2026 at 8:23 pm

    edit: Find an “out of the box” example at the end of the post!

    Because you see multiple rows affected I guess it has something to do how the system recycles resources and maybe the reference to the Button is ambiguous.

    I’m not sure where I picked up this practise (either Android Tutorials or our former developer who learned android via these tutorials). However the suggestion is to use a nested class ViewHolder

    private static class ViewHolder {
    
        public Button processCheck;
    }
    

    Place this in the Adapter or whatever class your getView() is declared in and modify it as such:

    public View getView(int position, View convertView, ViewGroup parent) {
    
        View v = convertView;
        $ViewHolder viewHolder; //wait for the magic!
        if (v == null) {
            v = li.inflate(R.layout.process_row, null);
            $viewHolder = new ViewHolder();
            $viewHolder.processCheck = (Button) v.findViewById(R.id.processCheck);
            $v.setTag(viewHolder);
            //ok we somewhat stuffed an object with the Button into our View... so what?
        }
    
        $viewHolder = (ViewHolder) v.getTag();
        //see explanation below
    
        $viewHolder.processCheck.setOnClickListener(new View.OnClickListener() {
        //set the onClickListener for this and only this button.
            @Override
            public void onClick(View v) {
                if (process.isChecked() == false) {
                    process.setChecked(true);
                    processCheck.setBackgroundColor(Color.BLUE);
                }
                else {
                    process.setChecked(false);
                    processCheck.setBackgroundColor(Color.RED);
                }
    
            }
        });
    
        return v;
    }
    

    (I marked changes with $. I think this way they should be easy to spot if I use eclipse. Otherwise Find+Replace ^^)

    So getTag() will return an Object associated with the view called on. Object is so incredible generic you can call it to the ViewHolder object you’ve created. Therefore you can reference the button and store it in the object.
    Now whenever you get a ListView you can retrieve the Object and set the OnclickListener fresh and new. This way you’re only one Listener is called.

    Notice that you don’t have to call v.setTag(); again to “save” your changes.

    A pitfall might be if you do crazy stuff like inflating different layout in the same list for what reason ever. You might be tempted to assign different ViewHandlers to them like:

    if (v == null) {
        if( someCriteria) {
            v = li.inflate(R.layout.process_row, null);
            viewHolder = new ViewHolder(); 
        }
            v = li.inflate(R.layout.process_another_row, null);
            viewHolder = new AnotherViewHolder(); 
    }
    

    Then an unchecked call of getView() to one ViewHolder or another will likely throw an error at runtime!

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

Sidebar

Related Questions

I have a listView using a custom adapter. Each row contains a button and
I have created a custom listview that is populated using a row.xml file. Each
I want to have a list view with each row having two buttons like
I have an activity with a listView, which uses a custom row layout defined
I have an activity, where the ListView holds customized linear layout elements for each
I have designed a listview by inflating each row using inflater and I have
I want to have a list of elements (using a ListView) and each element
I have a listview which I am populating with a custom SimpleCursorAdapter, each row
I have a custom listview row that contains a number of textView components. Instead
I have an Activity in Android, with two elements: EditText ListView When my Activity

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.