gpt4 book ai didi

java - Android 通过 onSavedInstance 方法中的包传递列表

转载 作者:行者123 更新时间:2023-12-01 12:59:29 25 4
gpt4 key购买 nike

当方向改变时我需要保存一个列表。为此,我想通过 onSavedInstanceState 传递数据。方法,但是,我不知道如何执行此操作,请参阅下面的示例代码:

private List<EarSrt> leftAnswerList;
private List<EarSrt> rightAnswerList;

@Override
protected void onSaveInstanceState (Bundle outState){
super.onSaveInstanceState(outState);
outState.putCharSequenceArrayList("left_ear", leftAnswerList);
outState.putCharSequenceArrayList("right_ear", rightAnswerList);
}

此代码不起作用,因为我正在传递自定义列表。

错误:The method putCharSequenceArrayList(String, ArrayList<CharSequence>) in the type Bundle is not applicable for the arguments (String, List<EarSrt>)

如何将此数据传递到 bundle 中?我使用 Parcelable ?如果是这样,我将如何使用 ParcelableonSavedStateChanged方法?

另外,如何检索数据,我是否使用 onRestoreInstanceState方法?如果是这样,我将如何去做?

感谢您的帮助。

编辑:

所以我刚刚意识到我可以通过以下代码传递它:

@Override
protected void onSaveInstanceState (Bundle outState){
super.onSaveInstanceState(outState);
outState.putParcelable("left_ear", (Parcelable) leftAnswerList);
outState.putParcelable("right_ear", (Parcelable) rightAnswerList);

}

我需要使用 onRestoreInstanceState方法或者我可以在 onCreate 中说方法if(savedInstanceState != null)然后做点什么?

编辑:

onCreate方法:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.hearing_test);

ActivityHelper activityHelper = new ActivityHelper(this);

activityHelper.setTitleTextSize(R.string.Hearing_Test, true);
isPhone = activityHelper.isPhone();

if(isPhone){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

View infoView = (View)findViewById(R.id.info_button);
infoView.setVisibility(View.INVISIBLE);
notUnderstoodButton = (Button) findViewById(R.id.not_understood_button);
repeatButton = (Button) findViewById(R.id.repeat_button);
progressTextView = (TextView) findViewById(R.id.progressTextView);

progressTextView.setText("Left ear: Step 1 of 9");

notUnderstoodButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundButtonClicked(v);
}
});

repeatButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
noisePlayer.seekTo(0);
testPlayer.seekTo(0);

noisePlayer.start();
testPlayer.start();

disableButtons();
}
});

panLeft = false;
initialiseArrays();
getNextTest(-1);
}

最佳答案

要通过Bundle传递数据,它需要实现Parceable接口(interface)。在您的情况下,这意味着 EarSrt 必须是 Parceable 因为您正在使用它的 List 。请参阅herehere如何实现Parceable

要恢复数据,使用 onRestoreInstanceStateonCreate 都没有关系。引用自doc :

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

关于java - Android 通过 onSavedInstance 方法中的包传递列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23630336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com