如何使用保存的istance状态和Android的恢复与纺纱实例的状态? [英] How to use saved istance state and restore instance state with spinners in in Android?

查看:94
本文介绍了如何使用保存的istance状态和Android的恢复与纺纱实例的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的活性,使其具有一个选项列表,

I have a spinner in my activity with a list of options,

当用户选择一个选项,我用OnItemSelected适配器获得所选选项的位置值和内我增加了一些如else语句。如果语句,竟产生从TextView中的predefined数据库并显示一个随机字符串。

When the user selects an option, I have used OnItemSelected adapter to get the position value of the selected option and within that i have added some If Else Statements. The If statement, actually generates a random string from the predefined database and displays on TextView.

现在我将切换回另一个活动,并再次回到这个活动。现在的问题是默认微调的第一个选项是唯一入选的,我需要的用户最后选择的选项,当活动再次启动该选项的功能,必须运行更新。

Now i will be switching back to another activity and again come back to this activity. Now the problem is by default the first option of the spinner is only selected, I need to update with the last selected option of the user and when the activity starts again that option's functions must be run.

我做了一些搜索过,发现这可以通过使用的onSaveInstanceState onRestoreInstanceState 现在已经完成了:

I did some searches too and found that this can be done by using onSaveInstanceState and onRestoreInstanceState now :


  • 如何使用这两个,我应该在这里把他们的onItemSelected(适配器)或外部?
  • 里面
  • 如何更新(或存储)由用户选择的随机生成的code?
  • 新的位置值
  • How do I use these two and where should i put them, inside the onItemSelected(adapter) or Outside?
  • How do I update(or store) the new position value selected by the user to the random generated code?

为了清楚起见,请查看我的code:

For clarity please check my code:

@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
     }

EDITED问题:

我已经编辑更加清晰的问题。基本上我所要做的是,我要救他们由用户选择的位置,以微调。当用户点击三江源按钮活动将完成,并通过不同的类广播接收器叫做几分钟后就会开始。当活动再次开始用户的最后一个所选选项(previous活动)必须为默认在此活动

I have edited the question for more clarity. Basically what I am trying to do is, I want to save the position selected by they user, with the spinner. When the user clicks "Thankyou" Button the activity will finish and will start after few minutes called by a Broadcast receiver from a different class. When the activity starts again the last selected option (previous activity) of the user must be as default in this activity.

我想,如果你看一下code中的问题在保存和恢复实例状态。请您能有所帮助。先谢谢了。

I think if you look at the code the problem is in save and restore instance state. Please can you help. Thanks in advance.

请看看我的code:

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);
}


}

这是活动的样子

推荐答案

在您的present活动覆盖了一个名为方法的onSaveInstanceState(),把你的数据包

In your present Activity override a method called onSaveInstanceState() and put your data in bundle.

和覆盖另一种方法 onRestoreInstanceState(),并从包获取这些值....

and override another method onRestoreInstanceState() and get those values from the bundle....

这篇关于如何使用保存的istance状态和Android的恢复与纺纱实例的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆