Java 更改屏幕方向时,如何保存字符串数组的随机位置?

Java 更改屏幕方向时,如何保存字符串数组的随机位置?,java,android,arrays,random,Java,Android,Arrays,Random,我无法通过标题解释我想说什么,但我会在这里展开 无论如何,我还在学习如何编码,所以请原谅我对一些术语的误用 我制作了一个显示字符串数组的简单应用程序。它只包含两个可视部分——一个TextView元素和一个Button元素。按下按钮将显示字符串数组中的随机结果 问题是,当我改变方向时,显示的水果会改变。我知道这是因为每当方向改变时,活动都会“重新制作”。我确实了解如何使用onSaveInstanceState及其对应项 但是我不知道如何保存和恢复显示中当前显示的值,因为我使用的是Random而不是

我无法通过标题解释我想说什么,但我会在这里展开

无论如何,我还在学习如何编码,所以请原谅我对一些术语的误用

我制作了一个显示字符串数组的简单应用程序。它只包含两个可视部分——一个TextView元素和一个Button元素。按下按钮将显示字符串数组中的随机结果

问题是,当我改变方向时,显示的水果会改变。我知道这是因为每当方向改变时,活动都会“重新制作”。我确实了解如何使用onSaveInstanceState及其对应项

但是我不知道如何保存和恢复显示中当前显示的值,因为我使用的是Random而不是int。因此,我认为我不能依赖于使用putInt和getInt

TextView Display;
Button randomize;
String[] fruitArray;
Random counter = new Random();
Random rand = new Random();
int launchCounter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initializeButtons();
    checkAndroidVersion();
    changeFont();

    fruitArray= getResources().getStringArray(R.array.fruits);

    launchCounter = rand.nextInt(10) + 1;

    Display.setText(fruitArray[launchCounter]);

    randomize.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            int maxIndex = jokeArray.length;
            int generatedIndex = counter.nextInt(maxIndex);
            Display.setText(fruitArray[generatedIndex]);

        }

    });
请尝试以下代码段:

@Override    
protected void onSaveInstanceState(Bundle icicle) {
  super.onSaveInstanceState(icicle);
  icicle.putString("position", launchCounter);
}
And restore the values in onCreate():

@Override
public void onCreate(Bundle icicle) {
  ...
  if (icicle != null){
    value = icicle.getString("position");
  }
}

您可以使用onRestoreInstanceState,而不是在onCreate中恢复数据。区别在这里解释:

您可以尝试定义generatedIndex static globaly,也可以使用首选项保存随机数

在首选项中保存您的值,如下所示:-

SharedPreferences mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mSharedPrefs.edit().putInt("KEY", generatedIndex).commit();
在onCreate期间,您可以使用mSharedPrefs.getIntKEY获取保存的值,并在TextView中显示它。

首先,您需要@Override您的onSaveInstanceState方法。您可以对代码执行以下操作

// This bundle will be passed to your onCreate method when it is called
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putInt( "LaunchCounter", launchCounter );
}
现在定义这一行:

launchCounter = rand.nextInt(10) + 1;
应将其替换为以下内容:

// If this is re-creating then it will not be null.         
// If it has just been initialised, it will be.
if ( savedInstanceState != null ) 
{
    launchCounter = savedInstanceState.getInt( "LaunchCounter" );
} 
else 
{
    launchCounter = rand.nextInt(10) + 1;
}

您可以用静态字符串引用替换硬编码字符串。

因为您正在数组中存储水果名称,所以可以在调用onSaveInstanceState时存储当前显示的水果的数组索引。在onRestoreInstanceState中,然后可以使用getInt字符串键int defaultValue检索当前水果的索引

如果不存在保存的实例状态,则将-1指定给FROUT index变量;如果保存的实例状态中未存储任何实例,则将其指定为默认值;如果变量为-1,则只需生成一个新的随机索引;如果不存在,则将正确的文本指定给TextView

public class MainActivity extends Activity {

   TextView labelFruit;
   Button buttonCycle;

   String[] fruitArray;
   Random rand = new Random();
   int fruitIndex = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fruitArray = getResources().getStringArray(R.array.fruits);

        labelFruit = (TextView) findViewById(R.id.text_fruit);
        buttonCycle = (Button) findViewById(R.id.button_cycle);

        buttonCycle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fruitIndex = rand.nextInt(fruitArray.length);
                labelFruit.setText(fruitArray[fruitIndex]);
            }
        });
    }

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

        if (savedInstanceState == null) {
            fruitIndex = -1;
        } else {
            fruitIndex = savedInstanceState.getInt("FRUIT_INDEX", -1);
        }

        if (fruitIndex == -1) {
            fruitIndex = rand.nextInt(fruitArray.length);
        }

        labelFruit.setText(fruitArray[fruitIndex]);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("FRUIT_INDEX", fruitIndex);
    }
}