如何在Android中使用保存实例状态并使用微调器恢复实例状态?

时间:2011-07-29 08:01:04

标签: java android spinner

我的活动中有一个微调器,有一个选项列表,

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

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

我也进行了一些搜索,发现现在可以使用onSaveInstanceStateonRestoreInstanceState完成此操作:

  • 如何在onItemSelected(适配器)或Outside外部使用这两个以及我应该放在哪里?
  • 如何更新(或存储)用户选择的新位置值到随机生成的代码?

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

@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时,活动将完成,并且将在几分钟后由来自不同班级的广播接收器调用。当活动再次开始时,用户的最后一个选定选项(上一个活动)必须在此活动中作为默认选项。

我认为如果你看一下代码,问题就在于保存和恢复实例状态。

请查看我的代码:

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

1 个答案:

答案 0 :(得分:1)

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

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