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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:17:00+00:00 2026-05-24T18:17:00+00:00

I’m making my first Android application and I’m having a problem for which I

  • 0

I’m making my first Android application and I’m having a problem for which I can’t find the answer anywhere on Google.

I want a list of items with checkboxes. I want both the item itself and the checkbox to be clickable.

    public class MyItem extends ListActivity {
        private ArrayList<MyItem> items;
        public void onCreate(Bundle savedInstanceState) {
            /* code which creates instances of MyItem and inserts them on the *list* variable */
        MyArrayAdapter adapter = new MyArrayAdapter(this, R.layout.my_item, list);

        setListAdapater(adapter);
        setContentView(R.layout.items_list);
    }
        public onListItemClick(ListView l, View v, int position, long id){
            //handles the click on an item
        }

    public class MyArrayAdapter extends ArrayAdapter<MyItem>{
        private MyItem item;
        public MyArrayAdapter(Context context, int resourceId, ArrayList<MyItem> list){
            //code for the constructor
        }
        public getView(int position, View convertView, ViewGroup parent){
            convertView = inflater.inflate(resourceId, null);


            this.item = list.get(position);
            if (this.item == null) {
                return convertView;
            }
            else{
                if (resourceId == R.layout.my_item) {
                    final CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkbox);

                    if(cb != null){
                        //initially
                        if(chosen)
                            cb.setChecked(true);
                        else
                            cb.setChecked(false);
                        //set listener
                        cb.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View arg0) {
                                if(cb.isChecked())
                                    chosen = true;
                                else
                                    chosen = false;
                            }
                        });
                    }
                }
            return convertView;
        }
    }    
}

Don’t worry about the chosen variable. I wrote that to simply the code. It actually corresponds to a value in a database. The clicking on an item works just fine. However when I click on a checkbox what happens is this:

  • the checkbox in which I clicked appears selected (this is the work of the Android’s UI)
  • the checkbox that internally is checked is the last one on the screen whichever it is, i.e., if I my screen displays 8 items and I click in one of them (doesn’t matter which one) the check appears in the correct checkbox but internally, the 8th item is the one getting checked.

I would appreciate any help you could provide me. Thanks in advance.

  • 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-24T18:17:02+00:00Added an answer on May 24, 2026 at 6:17 pm

    Actually, the implementation backing chosen is key. Android does some optimization with list views to allow you to reuse the list item views to avoid excessive object creation and garbage collection (which would often lead to jerky scrolling). As such, you have to make sure that whenever relevant, you know exactly which list item you’re working with.

    Let’s say that you have a 100 list items. Your screen is probably not going to be able to display all of them. You might only show ten items at a time. So 10 views (really view hierarchies) are created to display those visible items. When you scroll down to the next ten items, instead of creating 10 new views (for a total of 20), the list might only create one more (to cover the case where half of one item is showing at the top and half of one is showing at the bottom of the screen for a total of 11 items visible on the screen) and the rest of items reuse the views created before.

    So a conceptual table representing the first screen might look like this:

    Item     View
    -------  --------
    Item 1   View 1
    Item 2   View 2
    Item 3   View 3
    Item 4   View 4
    Item 5   View 5
    Item 6   View 6
    Item 7   View 7
    Item 8   View 8
    Item 9   View 9
    Item 10  View 10
    

    And for after scrolling down ten items, it might look a little like this (probably not exactly, but this gets you the idea):

    Item     View
    -------  --------
    Item 11  View 1
    Item 12  View 2
    Item 13  View 3
    Item 14  View 4
    Item 15  View 5
    Item 16  View 6
    Item 17  View 7
    Item 18  View 8
    Item 19  View 9
    Item 20  View 10
    

    So what you can gather from this is that a single given view can represent different items as you scroll around. This means that your event handlers have to be a little more dynamic in how they find the item they’re related to.

    All this is to give you a bit of background so that you can change how you’re implementing your getView method. Here’s your actual problem: the variable item is in the scope of your Adapter. Unfortunately, I’m guessing that your code that you haven’t posted here that you’ve replaced with chosen uses item. You set item whenever an item view gets created. This means that after those first 8 views are created, item is set to the 8th item in your list. Whenever you click on a checkbox, you’re using item which is the 8th item and not the item that corresponds to the list item view that you clicked.

    Here’s the structure for getView that I’d recommend:

    public getView(int position, View convertView, ViewGroup parent){
            View view = convertView;
            if (view == null) {
                view = inflater.inflate(R.layout.my_item, null);
            }
    
            final MyItem item = list.get(position);
            final CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkbox);
            // This stores a reference to the actual item in the checkbox
            cb.setTag(item);
    
            if(item.chosen)
                cb.setChecked(true);
            else
                cb.setChecked(false);
    
            //set listener
            cb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // This gets the correct item to work with.
                    final MyItem clickedItem = (MyItem) view.getTag();
                    if(cb.isChecked())
                        clickedItem.chosen = true;
                    else
                        clickedItem.chosen = false;
                }
            });
    
            return view;
        }
    }
    

    Note that I’ve gotten rid of the class-level item variable.

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the

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.