0

When i click on the first item of ListView, the first item is selected. But when i scroll down, the 12th item is selected too, but i didn;t click on the 12th item. Why it was happend?

First screen: http://oi59.tinypic.com/9iw7b8.jpg Second screen : oi60.tinypic.com/1zxunv4. jpg [delete whitespace]

My sourse code is like that : http://startandroid.ru/en/uroki/vse-uroki-spiskom/85-urok-44-sobytija-v-listview.html

But i add the white recolor after utem click.

Den
  • 11
  • 1
  • consider providing full code of your `getView()` method – Oleg Osipenko May 28 '15 at 21:33
  • I think you are facing a similar problem I had a couple of days ago: http://stackoverflow.com/questions/30424912/change-item-background-color-in-simple-listview – Ruocco May 28 '15 at 23:27

1 Answers1

1

This question was answered here: Checking a checkbox in listview makes other random checkboxes checked too

Basically, when you scroll down you list, it recycles its present state as well as listeners attached to it.

One way I solved this problem is (suposing that your list is called check in java):

  • Create a boolean array (let's name it listCheck), same size as your checkbox, all values false
  • Write in your adapter getView method:

    check.setChecked(listCheck[position]); //listCheck is your array of booleans.
    check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkEspecialidade);
            listCheck[position] = check.isChecked();
        }
    });
    

    Basically, we set the check value the same as the one on the array, and when the user clicks it, we also change de value of that check in the array. That works because the boolean array is not recycled.

Community
  • 1
  • 1