gpt4 book ai didi

java - 如何在 Android 中使用已保存的实例状态和通过微调器恢复实例状态?

转载 作者:行者123 更新时间:2023-11-30 04:35:50 28 4
gpt4 key购买 nike

我的 Activity 中有一个带有选项列表的微调器,

当用户选择一个选项时,我使用 OnItemSelected 适配器获取所选选项的位置值,并在其中添加了一些 If Else 语句。 If 语句实际上从预定义的数据库中生成一个随机字符串并显示在 TextView 上。

现在我将切换回另一个 Activity ,然后再次回到这个 Activity 。现在的问题是默认情况下仅选择微调器的第一个选项,我需要使用用户最后选择的选项进行更新,并且当 Activity 再次启动时必须运行该选项的功能。

我也做了一些搜索,发现现在可以通过使用 onSaveInstanceStateonRestoreInstanceState 来完成:

  • 我如何使用这两个以及我应该把它们放在哪里,是在 onItemSelected(adapter) 内部还是外部?
  • 如何将用户选择的新位置值更新(或存储)到随机生成的代码中?

为了清楚起见,请检查我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scheduledactivity);

/**displayed when the activity is launched (this should be
updated with the user selected option)**/
Resources res = getResources();
myString = res.getStringArray(R.array.normal);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);

//initiating the spinner
Spinner s = (Spinner) findViewById( R.id.spinner);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 1){

//displayed on the time when user selects the option
Resources res = getResources();
myString = res.getStringArray(R.array.hardships);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);

}

else {
//do nothing
}

已编辑问题:

为了更清楚,我已经编辑了问题。基本上我想做的是,我想用微调器保存用户选择的位置。当用户单击“Thankyou”Button 时, Activity 将结束,并在几分钟后由来自不同类的广播接收器调用后开始。当 Activity 再次开始时,用户最后选择的选项(先前的 Activity )必须是此 Activity 中的默认选项。

我认为如果您查看代码,问题出在保存和恢复实例状态上。

请看我的代码:

import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MyScheduledActivity extends Activity {

private String[] myString;
private static final Random rgenerator = new Random();
protected int mPos;
protected String mSelection;
String situation[] = {"Normal","Hardship","Sadness","Exam"};

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scheduledactivity);


if (mPos == 0) {
Resources res = getResources();
myString = res.getStringArray(R.array.normal);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);
}

//selection of the spinner
Spinner s = (Spinner) findViewById( R.id.spinner);

// Application of the Array to the Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, situation);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(spinnerArrayAdapter);

s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

MyScheduledActivity.this.mPos = pos;
MyScheduledActivity.this.mSelection = parent.getItemAtPosition(pos).toString();

//display toast
Toast.makeText(parent.getContext(), "The Situation is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

if (mPos == 0){

Resources res = getResources();
myString = res.getStringArray(R.array.hardships);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);

}

if (mPos == 1){

Resources res = getResources();
myString = res.getStringArray(R.array.hardships);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);

}

else {
//do nothing
}

}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
//do something else
return;
}
});


//dismiss button - Thank You
Button buttonDismiss = (Button)findViewById(R.id.dismiss);

buttonDismiss.setOnClickListener(new Button.OnClickListener(){

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

}

@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putStringArray(mSelection, situation);
super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
situation = savedInstanceState.getStringArray("mSelection");
super.onRestoreInstanceState(savedInstanceState);
}


}

This is how the activity looks

最佳答案

在您当前的 Activity 中覆盖一个名为 onSaveInstanceState() 的方法并将您的数据放入包中。

并覆盖另一个方法 onRestoreInstanceState() 并从包中获取这些值....

关于java - 如何在 Android 中使用已保存的实例状态和通过微调器恢复实例状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6870224/

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