Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, the implementation backing <code>chosen</code> 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.</p> <p>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.</p> <p>So a conceptual table representing the first screen might look like this:</p> <pre> 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 </pre> <p>And for after scrolling down ten items, it might look a little like this (probably not exactly, but this gets you the idea):</p> <pre> 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 </pre> <p>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.</p> <p>All this is to give you a bit of background so that you can change how you're implementing your <code>getView</code> method. Here's your actual problem: the variable <code>item</code> is in the scope of your <code>Adapter</code>. Unfortunately, I'm guessing that your code that you haven't posted here that you've replaced with <code>chosen</code> uses <code>item</code>. You set <code>item</code> whenever an item view gets created. This means that after those first 8 views are created, <code>item</code> is set to the 8th item in your list. Whenever you click on a checkbox, you're using <code>item</code> which is the 8th item and not the item that corresponds to the list item view that you clicked. </p> <p>Here's the structure for <code>getView</code> that I'd recommend:</p> <pre><code>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; } } </code></pre> <p>Note that I've gotten rid of the class-level <code>item</code> variable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload