gpt4 book ai didi

android - 恢复自定义阵列适配器中的复选框状态

转载 作者:行者123 更新时间:2023-11-30 03:02:02 24 4
gpt4 key购买 nike

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

 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 但似乎其中一个或两个都没有被调用

最佳答案

onSaveInstanceState 在您需要从配置更改、低内存状态、长时间处于后台等情况下恢复时很有用。但是如果您真的终止任务并重新启动应用程序,您将需要一个持久存储机制。

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

SQLite 可能是您正在寻找的 CheckedModel;带有名称字段和 bool 检查字段的单个表。但这绝不是唯一的方法。

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

关于android - 恢复自定义阵列适配器中的复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22380771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com