如何在Android中使用保存的实例状态和恢复带有微调的实例状态?

2022-04-01 00:00:00 spinner android java

我的活动中有一个带有选项列表的微调工具

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

现在我将切换回另一个练习,并再次返回到此练习。现在的问题是,默认情况下,微调控件的第一个选项仅处于选中状态,我需要使用用户最后选择的选项进行更新,当活动再次启动时,必须运行该选项的功能。

我也做了一些搜索,发现可以使用onSaveInstanceStateonRestoreInstanceStateNOW:

  • 如何使用这两个?应该将它们放在哪里:onItemSelected(适配器)内部还是外部?
  • 如何将用户选择的新位置值更新(或存储)为随机生成的代码?

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

@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


解决方案

在当前活动中,重写名为onSaveInstanceState()的方法,并将数据放入包中。

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

相关文章