How to retain the selected item in spinner? [closed]

為{幸葍}努か 提交于 2019-12-13 08:16:13

问题


I am now learning android. I am working on an application, where in when i click on a value of Spinner it moves to the next Intent. And then when I come back to the old (first intent), i need to retain the selected item in that spinner. Please help me out. Thanks in advance


回答1:


Create a static variable in activity, declare it globally. Save the value of selected item on click of it. On onCreate() method of your activity, check whether the value is null, if not load that value in to spinner and make it selected. Also make static variable null once you use that.

UPDATE:

Create a global static variable as follows in your activity.

static String valueSelected=null;

in onCreate() method write the code as follows.

onCreate(){
     setContentView();
     ....
     if(valueSelected!=null){
          sp.setSelected(valueSelected);
          valueSelected=null;
     }

}

and in onClick() listener set the value to valueSelected as follows.

onClick(){
valueSelected=sp.spinner.getSelectedItem().toString();
}

Where sp is spinner



来源:https://stackoverflow.com/questions/28184728/how-to-retain-the-selected-item-in-spinner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!