Bundle之间发送ArrayList<Integer>
Send ArrayList<Integer> between Bundle
问题:
如何将_selectednumber
和_points
的数据从onSaveInstanceState
转移到onRestoreInstanceState
?
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");
}
以下应该适合您:
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");
}
您可以使用 putIntegerArrayList()
存储数据,并使用 getIntegerArrayList()
检索数据。但是,您已将变量声明为 List<Integer>
,这不满足 putIntegerArrayList()
.
的要求
你有两个选择。首先,您可以更改声明变量的方式,使它们显式 ArrayList
s,而不仅仅是 List
s:
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");
}
问题:
如何将_selectednumber
和_points
的数据从onSaveInstanceState
转移到onRestoreInstanceState
?
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");
}
以下应该适合您:
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");
}
您可以使用 putIntegerArrayList()
存储数据,并使用 getIntegerArrayList()
检索数据。但是,您已将变量声明为 List<Integer>
,这不满足 putIntegerArrayList()
.
你有两个选择。首先,您可以更改声明变量的方式,使它们显式 ArrayList
s,而不仅仅是 List
s:
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");
}