dimanche 21 février 2016

Checkbox value changes when scroll the listView

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.mCheckBoxes.add(holder.checkbox);
    holder.checkbox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

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

            if (((CheckBox) v).isChecked()) {
                for (int i = 0; i < holder.mCheckBoxes.size(); i++) {

                    if (holder.mCheckBoxes.get(i) == v) {
                        addPreferencesAndDataBase(country.getName(), country.getUrl());
                    } else {

                    }
                }
            } else {
                removePreferencesAndDataBase(country.getName());

            }
        }

    });

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

    return convertView;

}

}




Aucun commentaire:

Enregistrer un commentaire