本文介紹了如何在Android中使用保存的實例狀態和恢復帶有微調的實例狀態?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我的活動中有一個帶有選項列表的微調工具
當用戶選擇一個選項時,我使用OnItemSelected適配器來獲取所選選項的位置值,并在其中添加了一些If Else語句。If語句實際上從預定義的數據庫生成一個隨機字符串并顯示在TextView上。
現在我將切換回另一個練習,并再次返回到此練習。現在的問題是,默認情況下,微調控件的第一個選項僅處于選中狀態,我需要使用用戶最后選擇的選項進行更新,當活動再次啟動時,必須運行該選項的功能。
我也做了一些搜索,發現可以使用onSaveInstanceState
和onRestoreInstanceState
NOW:
如何使用這兩個?應該將它們放在哪里: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()
并從包中獲取這些值…
這篇關于如何在Android中使用保存的實例狀態和恢復帶有微調的實例狀態?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,