我在片段中有一个自定义列表。然后我用另一个列表替换片段。
我想当我回到我以前的列表(由于替换片段而被破坏)时恢复列表的最后状态(如选中的项目或微调器选定的项目)。我知道我必须使用onSaveInstanteState
并将列表的最后一个状态放入其中,然后从onActivityCreated
bundle 中获取它们。问题是我的列表很复杂,并且有一个 bean 类和 CustomAdapter。我想我可以使用下面的代码:
(我用过private List<NameBean> items;
):
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
// Retrive Data from list
for (NameBean bean : items) {
/* bean gets the orders values from NamesAdapters and MainActivity.
But the orders like "isSelected" (value of a boolean which is defined here in upper block) or
"IsSelected" and "SetSelected" which gets and sets the value of the boolean "selected" in
NameBean.java (Setters and getters) in conditional (boolean) format (true or false).
We can access them in MainActivity and NamesAdapter */
if (bean.isSelected()) {
sb.append(bean.getName() + ": " + bean.getSpinnerItem());
sb.append("\n");
}
}
showAlertView(sb.toString().trim());
}
我的 NameBean 类如下:
public class NameBean {
private String name;
private String spinnerItem;
private SpinnerAdapter spinnerAdapter;
private boolean selected;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public SpinnerAdapter getSpinnerAdapter() {
return spinnerAdapter;
}
public void setSpinnerAdapter(SpinnerAdapter spinnerAdapter) {
this.spinnerAdapter = spinnerAdapter;
}
public String getSpinnerItem() {
return spinnerItem;
}
public void setSpinnerItem(String spinnerItem) {
this.spinnerItem = spinnerItem;
}
}