如何在 class 中存储数组列表?

How can I store Arraylists in class?

我有一个 class data,用来存储我的用户数据。

public class deck {
    public static ArrayList deck = new ArrayList();
    public static ArrayList cardchosen = new ArrayList();
    public static ArrayList deck1Image = new ArrayList();
    public static ArrayList deck2Image = new ArrayList();
}

如何在 onSaveInstanceState 中保存这些数组的状态? 我必须使用不同的东西吗?

在数据中实现 Serializable 接口会更容易 class

public class Deck implements Serializable {

    public static ArrayList deck = new ArrayList();
    public static ArrayList cardchosen = new ArrayList();
    public static ArrayList deck1Image = new ArrayList();
    public static ArrayList deck2Image = new ArrayList();
}

然后像在 onSaveInstanceState

中那样设置 bundle

科特林

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        val deck = Deck()
        outState.putSerializable("mydeck", deck)
        super.onSaveInstanceState(outState, outPersistentState)
    }

Java

@Override
public void onSaveInstanceState(Bundle outState) {
   outState.putSerializable("mydeck", deck);
   super.onSaveInstanceState(outState);
}

需要隐式声明ArrayList的类型:

public class deck {
    public static ArrayList deck = new ArrayList<Integer>();
    public static ArrayList cardchosen = new ArrayList<Integer>();
    public static ArrayList deck1Image = new ArrayList<Integer>();
    public static ArrayList deck2Image = new ArrayList<Integer>();
}

编译器在您尝试执行时不知道:

ArrayList myArrayList = outstate.getIntegerArrayList("My Key")