由objets组成的arrayList的SaveInstance和RestoreInstance

问题描述

@H_404_0@我有这样的习惯清单

private  List<BeanHabitude> list;
@H_404_0@对象BeanHabitude具有一个计数器,用于记录用户养成习惯的次数

public int getNbFois() {
    return nbFois;
}
@H_404_0@我有以下两种保存和恢复方法

@Override
public void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
    state.putSerializable("habitudes",(Serializable) list);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    txtNomUtilisateur.setText(savedInstanceState.getString("courriel"));

    this.list.clear();
    this.list.addAll((ArrayList<BeanHabitude>) savedInstanceState.getSerializable("habitudes"));
@H_404_0@这是我的应用程序的创建。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_habitudes);
    btnPartager = findViewById(R.id.btnPartager);
    btnSuivant = findViewById(R.id.btnSuivant);
    btnConfiguration = findViewById(R.id.btnConfiguration);
    btnPrecedent = findViewById(R.id.btnPrecedent);
    btnAjoute = findViewById(R.id.btnAjoute);
    txtNomUtilisateur = findViewById(R.id.txtNomUtilisateur);
    txtActivite = findViewById(R.id.txtActivite);
    txtCounteur = findViewById(R.id.txtCounteur);


    list=new ArrayList<BeanHabitude>();

    prefs = getSharedPreferences("habidute1",MODE_PRIVATE);
    Integer text1 = Integer.parseInt(prefs.getString("text1","0"));
    prefs = getSharedPreferences("habidute2",MODE_PRIVATE);
    Integer text2 = Integer.parseInt(prefs.getString("text2","0"));
    prefs = getSharedPreferences("habidute3",MODE_PRIVATE);
    Integer text3 = Integer.parseInt(prefs.getString("text3","0"));
    prefs = getSharedPreferences("counteurHabitudes",MODE_PRIVATE);
    habitudeCounteur = Integer.parseInt(prefs.getString("habitudeCounteur","0"));

    BeanHabitude beanHabitude = new BeanHabitude();
    beanHabitude.setNbFois(text1);
    beanHabitude.setNomHabitude("Lire un page sur c++");
    list.add(beanHabitude);

    beanHabitude = new BeanHabitude();
    beanHabitude.setNbFois(text2);
    beanHabitude.setNomHabitude("Coder 10 ligne on java");
    list.add(beanHabitude);

    beanHabitude = new BeanHabitude();
    beanHabitude.setNbFois(text3);
    beanHabitude.setNomHabitude("Fair 3 unit tests");
    list.add(beanHabitude);

    txtActivite.setText(list.get(habitudeCounteur).getNomHabitude());

    btnPartager.setonClickListener(this);
    btnSuivant.setonClickListener(this);
    btnAjoute.setonClickListener(this);
    btnPrecedent.setonClickListener(this);
    btnConfiguration.setonClickListener(this);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.COURRIEL);
    txtNomUtilisateur.setText("Habitudes pour " + message);
}
@H_404_0@我的应用程序创建不正确,我想在restoreInstance中填充列表,并且想要将列表保存在SaveInstance中。我该怎么办,非常感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)