-1

问题:
如何将数据传输到_selectednumber_pointsonSaveInstanceStateonRestoreInstanceState

private List<Integer> _selectednumber= new ArrayList<>();
private List<Integer> _points = new ArrayList<>();
private int _hit= 0;
private int _round = 1;


protected void onSaveInstanceState(Bundle out)
{
    super.onSaveInstanceState(out);

    out.putInt("p_hit", _hit);
    out.putInt("p_round", _round );
}


@Override
protected void onRestoreInstanceState(Bundle in)
{
    super.onRestoreInstanceState(in);

    _hit = in.getInt("p_hit");
    _round = in.getInt("p_round");
}
4

2 回答 2

0

您可以使用putIntegerArrayList()来存储数据,并getIntegerArrayList()检索它。但是,您已将变量声明为List<Integer>,这不满足putIntegerArrayList().

你有两个选择。首先,您可以更改声明变量的方式,使它们显式为ArrayLists,而不仅仅是Lists:

private ArrayList<Integer> _selectednumber= new ArrayList<>();
private ArrayList<Integer> _points = new ArrayList<>();
private int _hit= 0;
private int _round = 1;

protected void onSaveInstanceState(Bundle out)
{
    super.onSaveInstanceState(out);

    out.putInt("p_hit", _hit);
    out.putInt("p_round", _round );
    out.putIntegerArrayList("p_selectednumber", _selectednumber);
    out.putIntegerArrayList("p_points", _points);
}

@Override
protected void onRestoreInstanceState(Bundle in)
{
    super.onRestoreInstanceState(in);

    _hit = in.getInt("p_hit");
    _round = in.getInt("p_round");
    _selectednumber = in.getIntegerArrayList("p_selectednumber");
    _points = in.getIntegerArrayList("p_points");
}

或者,您可以在尝试将它们放入捆绑包时包装您的List实例:new ArrayList<>()

private List<Integer> _selectednumber= new ArrayList<>();
private List<Integer> _points = new ArrayList<>();
private int _hit= 0;
private int _round = 1;

protected void onSaveInstanceState(Bundle out)
{
    super.onSaveInstanceState(out);

    out.putInt("p_hit", _hit);
    out.putInt("p_round", _round );
    out.putIntegerArrayList("p_selectednumber", new ArrayList<>(_selectednumber));
    out.putIntegerArrayList("p_points", new ArrayList<>(_points));
}

@Override
protected void onRestoreInstanceState(Bundle in)
{
    super.onRestoreInstanceState(in);

    _hit = in.getInt("p_hit");
    _round = in.getInt("p_round");
    _selectednumber = in.getIntegerArrayList("p_selectednumber");
    _points = in.getIntegerArrayList("p_points");
}
于 2018-02-05T18:39:13.233 回答
0

下面应该为你工作:

protected void onSaveInstanceState(Bundle out) {
    super.onSaveInstanceState(out);
    out.putInt("p_hit", _hit);
    out.putInt("p_round", _round);
    out.getIntegerArrayList("_selectednumber");
    out.getIntegerArrayList("_points");
}


@Override
protected void onRestoreInstanceState(Bundle in) {
    super.onRestoreInstanceState(in);
    _hit = in.getInt("p_hit");
    _round = in.getInt("p_round");
    _selectednumber = in.getIntegerArrayList("_selectednumber");
    _points = in.getIntegerArrayList("_points");
}
于 2018-02-05T17:00:23.930 回答