在自定义数组适配器中恢复复选框状

时间:2014-03-13 13:53:40

标签: android checkbox android-listview android-arrayadapter

我正在尝试设置我的应用程序,以便在我终止任务并重新启动应用程序时仍会检查正确的复选框。应用程序进入后台似乎没问题,但当它完全存在时,所有复选框都是空白的。我尝试过一些东西,但似乎无法让它发挥作用。这是我的代码。

 public class SyncHomeFragment extends ListFragment {

    ListView listView;
    Button button;
    ArrayAdapterWithCheck adapter;
    boolean checkedState;

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.activity_sync_home, container, false);

        button = (Button) rootView.findViewById(R.id.confirm_button);

        adapter = new ArrayAdapterWithCheck(getActivity(), getModel());
        setListAdapter(adapter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO for test only remove
                boolean [] items =  adapter.getCheckBoxState();
                for(Boolean b : items){
                    /*String item = Boolean.toString(b);
                    Toast.makeText(getActivity().getBaseContext(), item, 0).show();*/
                    List<CheckedModel> checkedModels = adapter.getList();
                    String item = checkedModels.get(1).getName();
                    Toast.makeText(getActivity().getBaseContext(), item, 0).show();
                }
            }
        });

        return rootView;
    }

    @Override public void onSaveInstanceState(Bundle savedInstanceState) {
        boolean[] itemStates = adapter.getCheckBoxState();
        savedInstanceState.putBooleanArray("CheckBoxState", itemStates);
    }

    @Override public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if(savedInstanceState != null) {
            boolean[] checkedState = savedInstanceState.getBooleanArray("CheckBoxState");
            adapter.setCheckBoxState(checkedState);
        }
    }

    private List<CheckedModel> getModel() {
        List<CheckedModel> list = new ArrayList<CheckedModel>();
        list.add(get("Linux"));
        list.add(get("Windows7"));
        list.add(get("Suse"));
        list.add(get("Eclipse"));
        list.add(get("Ubuntu"));
        list.add(get("Solaris"));
        list.add(get("Android"));
        list.add(get("iPhone"));

        return list;
    }

    private CheckedModel get(String s) {
        return new CheckedModel(s);
    }

}

我的阵列适配器

public class ArrayAdapterWithCheck extends ArrayAdapter<CheckedModel> {

    private final List<CheckedModel> list;
    private final Activity context;
    private boolean [] checkBoxState;
    private ViewHolder viewHolder;

    public ArrayAdapterWithCheck(Activity context, List<CheckedModel> list) {
        super(context, R.layout.row_layout_with_check, list);
        this.context = context;
        this.list = list;
        checkBoxState = new boolean[list.size()];
    }

    static class ViewHolder {
        protected ImageView image;
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.row_layout_with_check, null);
            viewHolder = new ViewHolder();
            viewHolder.image = (ImageView) view.findViewById(R.id.icon);
            viewHolder.text = (TextView) view.findViewById(R.id.label);

            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    CheckedModel element = (CheckedModel) viewHolder.checkbox.getTag();
                    element.setSelected(buttonView.isChecked());
                    checkBoxState[position] = isChecked;

                    //save(list.get(position), checkBoxState[position]);
                    String item = Boolean.toString(checkBoxState[position]);
                    //TODO Remove
                    Toast.makeText(getContext(), item, 0).show();
                }
            });
            viewHolder.checkbox.setChecked(checkBoxState[position]);
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }

    public List<CheckedModel> getList() {
        return list;
    }

    public boolean[] getCheckBoxState() {
        return checkBoxState;
    }


    public void setCheckBoxState(boolean[] checkBoxState) {
        this.checkBoxState = checkBoxState;
        for(int i = 0; i<checkBoxState.length; i++){
            viewHolder.checkbox.setChecked(checkBoxState[i]);
        }
    }

}

我的模特

public class CheckedModel extends BasicModel {

    private boolean selected;

    public CheckedModel(String name) {
        super(name);
        selected = false;
    }

    public boolean isSelected() {
        return selected;
    }

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

有没有办法在应用程序被杀死并恢复后恢复每个复选框的状态?从环顾四周我想到使用onSaveInstanceState和onActivityCreated,但似乎有一个或两个都没有被调用

1 个答案:

答案 0 :(得分:1)

当需要从配置更改,低内存状态,后台长时间等恢复时,

onSaveInstanceState非常有用。但如果你真的杀了任务并重新启动应用程序,则需要持久存储机制。

http://developer.android.com/guide/topics/data/data-storage.html

SQLite可能就是您在CheckedModel上寻找的东西;具有名称字段和布尔选中字段的单个表。但这绝不是唯一的方法。

使用onSaveInstanceStateonActivityCreated执行持久性的方式对于在应用程序中保留数据绝对正确。只要您的应用程序正在运行(即使它已被放入后台),它就可以使用它来恢复CheckedModel。但是,如果您希望数据在应用程序之外保留,例如退出应用程序并返回,终止任务,重新启动设备等,那么您将需要使用如上所述的外部持久数据存储机制。 / p>