滚动和旋转时,ListView保持复选框状态

时间:2018-10-28 21:27:52

标签: java android

我的应用程序是在简化的情况下创建的,最终似乎事与愿违,因为我无法保持应用程序在滚动和旋转时的状态。我试图使用以下代码来保持复选框的状态,但是在我重复滚动超过2次左右之后,该复选框将不再被选中。它似乎只适用于第一卷。我见过其他人通过使用诸如视图持有人之类的东西来解决此问题,并在旋转时还原实例以维持条件,但我很想知道是否有针对我的问题的解决方案。我尝试包含尽可能短的代码

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    selections = new ArrayList<String>();
    button = (Button)findViewById(R.id.button);

    final mAdapter adapter = new mAdapter(this, imgid, cityname, countryname);
    LV = (ListView) findViewById(R.id.listview);
    LV.setAdapter(adapter);

    LV.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String selected = cityname[+position];
            CheckBox checkbox = (CheckBox)view.findViewById(R.id.checkBox);
            if (selections.contains(selected) || checkbox.isChecked()){
                selections.remove(selected);
                checkbox.setChecked(false);
            } else {
                selections.add(selected);
                checkbox.setChecked(true);
            }
        }
    });

mAdapter

public class mAdapter extends ArrayAdapter<String> {
private final Activity context;
private final Integer[] imgid;
private final String[] cityname, countryname;
private List<Integer> selectedItemsPositions = new ArrayList<>();

public mAdapter(Activity context, Integer[] imgid, String[] cityname, String[] countryname) {
    super(context, R.layout.row, cityname);
    this.context = context;
    this.imgid = imgid;
    this.cityname = cityname;
    this.countryname = countryname;
}

@Override
public View getView(final int position, View v, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.row, null, true);

    ImageView icon = (ImageView) rowView.findViewById(R.id.cityicon);
    TextView city = (TextView) rowView.findViewById(R.id.city);
    TextView country = (TextView) rowView.findViewById(R.id.country);
    final CheckBox checkbox = (CheckBox)rowView.findViewById(R.id.checkBox);

    icon.setImageResource(imgid[position]);
    city.setText(cityname[position]);
    country.setText(countryname[position]);

    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int position = (int)buttonView.getTag();
            if (!selectedItemsPositions.contains(position))
                selectedItemsPositions.add(position);
            else {
                selectedItemsPositions.remove(position);
            }
        }
    });

    checkbox.setTag(position);
    if (selectedItemsPositions.contains(position))
        checkbox.setChecked(true);
    else
        checkbox.setChecked(false);

    return rowView;
}
}

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

调用setChecked()会调用侦听器。这意味着,如果应该根据selectedItemsPositions检查项目,则会选中该项目,然后依次调用侦听器,该监听器将从selectedItemsPositions中删除位置。

解决方案:

在侦听器中,使用isChecked决定如何修改selectedItemsPositions。大概是:

        if (isChecked && !selectedItemsPositions.contains(position))
            selectedItemsPositions.add(position);
        else if (!isChecked && selectedItemsPositions.contains(position))
            selectedItemsPositions.remove(position);