0

在我的应用程序中,我有两个活动。在第一个活动中,我有两个微调器,当我在其中设置微调器值并进行下一个活动时。当我返回第一个活动时,数据将被刷新。我想要的是我想看看 privious 设置的 vales。我该怎么做。请任何人帮助我。

在这里,我的 Activity 生命周期如下所示:

1)当我从 Activity1 移动到 Activity2

暂停()

停止()

2)当我回到我的第一个活动时

开始()

onResume()

我已将值保存在 onSaveInstance 状态但未调用 onRestoreInstance 状态。这里我的代码是:@Override

protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        strAutoCompleteValue = autoPatientList.getSelectedItem().toString();
        strSpinnerAppointment = selectAppointment.getSelectedItem().toString(); 
        outState.putString("PatientName", strAutoCompleteValue);
        outState.putString("AppointmentDate", strSpinnerAppointment);

         Toast.makeText(this, "onSaved", 3000).show();
    }   

@Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        String pName = savedInstanceState.getString("PatientName");
        String appDate = savedInstanceState.getString("AppointmentDate");

       Toast.makeText(this, "onRestore", 3000).show();
    }

提前致谢。

4

4 回答 4

1

use onSaveInstanceState to save your spinner selection to a bundle. Then in onCreate(Bundle) when activity is recreated, get the data from the bundle to reset the spinners.

于 2013-06-20T08:02:43.840 回答
0

Try like this

Spinner Spinner_ = (Spinner) findViewById(R.id.Spinner_);

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
 {
            // TODO Auto-generated method stub
Spinner recent_Spinner = (Spinner) arg0;
Integer selected_Spinner_ = Spinner_.getSelectedItemPosition();
nw_global.set_Spinner_(selected_Spinner_.toString());
 }
 public void onNothingSelected(AdapterView<?> arg0) 
  {
    // TODO Auto-generated method stub
 }
if (nw_global.get_Spinner_() != null && !nw_global.get_Spinner_().isEmpty())
    {
        Spinner_.setSelection(Integer.parseInt(nw_global.get_Spinner_().replace("'", "")));
    } 
    else
    {
        Spinner_.setSelection(0);
    }

And do this in nw_global

public class nw_global extends Application {

private static String scale_spinner;
public static void set_Spinner_(String i) {
    // TODO Auto-generated method stub
    nw_global.scale_spinner = i;
}
public static String get_Spinner_(){

    return scale_spinner;
    }
 }

Just sample code here .. Hope it'll work

于 2013-06-20T08:27:53.913 回答
0

当你想编程时,Android你真的应该查看谷歌文档,因为有很多关于这些常见任务的信息。例如,这里介绍了这个特定问题:http: //developer.android.com/guide/topics/data/data-storage.html

当您查看Shared Preferences 部分时,您还可以找到示例代码。

于 2013-06-20T08:03:05.603 回答
0

您应该实现onSaveInstanceState将您的状态保存到 Bundle 并实现onRestoreInstanceStateonCreate来恢复您的状态。

于 2013-06-20T08:06:37.530 回答