滚动listView时,复选框值会更改

时间:2016-02-21 20:08:00

标签: android listview checkbox

我现在在这里我对同一主题有一些问题,但这些对我有用: When scrolling custom ListView, the checkbox value changes Checkbox gets unchecked when i scroll my custom listview

我可以将此任何答案应用于我的代码:

我的问题是,如果我选中或取消选中某个复选框并滚动列表视图,当我回到该项时没有正确的状态。

有人可以查看我的getView方法并尝试查看错误的位置吗?

提前致谢。

这是我的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;

}

这是我的国家级:

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;
    }

}

1 个答案:

答案 0 :(得分:0)

看起来您在单击时将复选框的状态保存到共享首选项,但您没有更新Country对象以反映该更改。如果每次单击都更新Country类中selected的新值,则应该没问题:

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

此外,您无需在检查侦听器中的复选框上调用setChecked()。选中该框就足以将复选框置于正确的状态。