1

I now that here I have some questions with the same topic, but any of this is working for me: When scrolling custom ListView, the checkbox value changes Checkbox gets unchecked when i scroll my custom listview

I can apply any of this answer to my code:

My problem is that if I check or uncheck some checkbox and scroll the listview, when I comeback to the item don't have his correct state.

Someone can I take a look to my getView method and try to see where is the error??

Thanks in advance.

This is my getView

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

    Log.e("ConvertView", String.valueOf(position));
    final ViewHolder holder;

    if (convertView == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.newspaper_list_row, null);

        holder = new ViewHolder();
        holder.newspaperName = (TextView) convertView.findViewById(R.id.newspaperName);
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);

        Country country = countryList.get(position);
        holder.checkbox.setTag(country.getName());

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.checkbox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            CheckBox cb = (CheckBox) v;
            Country country = (Country) cb.getTag();


           if (((CheckBox) v).isChecked()) {
                holder.checkbox.setChecked(true);
                addPreferencesAndDataBase(country.getName(), country.getUrl());

            } else {
                removePreferencesAndDataBase(country.getName());
                holder.checkbox.setChecked(false);

            }
        }

    });

    Country country = countryList.get(position);
    holder.newspaperName.setText(country.getName());
    holder.checkbox.setChecked(country.isSelected());
    holder.checkbox.setTag(country);

    return convertView;

}

This is my country class:

public class Country {

    int id;
    String name = null;
    String url = null;
    boolean selected = false;

    public Country(String name, String url, boolean selected) {
        super();
        this.name = name;
        this.url = url;
        this.selected = selected;
    }

    public Country(String name, String url) {
        super();
        this.name = name;
        this.url = url;
    }

    public long getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public boolean isSelected() {
        return selected;
    }
    public void setSelected(boolean selected) {
        this.selected = selected;
    }

}
Community
  • 1
  • 1
Simpson
  • 593
  • 5
  • 20
  • I don't understand what mCheckBoxes is used for, and also why you are iterating it inside the listener for a single checkbox. – Doug Stevenson Feb 21 '16 at 20:22
  • mCheckBoxes is an array of checkbox. I used it to set the correct checkbox in the database – Simpson Feb 21 '16 at 20:30
  • ok, I can remove this code, is not necessary... I just test it, but still having the problem of the checkbox when do a scroll. I just edit my question – Simpson Feb 21 '16 at 20:39

1 Answers1

0

It looks like you're saving the state of the checkbox to shared preferences when it's clicked, but you're not updating the Country object to also reflect that change. If you update the new value of selected in your Country class with each click, it should be OK:

CheckBox cb = (CheckBox) v;
Country country = (Country) cb.getTag();
country.setSelected(cb.isChecked());  // add this line

Also you don't need to call setChecked() on the checkbox in your check listener. Checking the box will is enough to put the checkbox in the correct state.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Not sure about you told me. In country class I have a get/set/ to can get the state... I updated my question with my country class. Can you take a look? Sorry, but I'm something new in Android. I'm not saving the state of the checkbox in the database, only in a sharedPreferences. Is not possible to save the state in a local variable only that remain when you do scroll?? – Simpson Feb 21 '16 at 21:15
  • I updated my answer to reflect the new information. You need to update the Country object to reflect the change to the checkbox. – Doug Stevenson Feb 21 '16 at 21:29