如何保持Android的微调状态状态、Android

2023-09-04 04:01:00 作者:我们都还小,根本不懂爱

我在做一个Android应用程序,它可以从一个Web服务与数据;加载到微调。我需要保持离心器的选择的数据状态,而我去一些画面+回来。举例来说,如果我收到来自Web服务的数据作为 1.apple 2.orange 3.grapes &放大器;加载到微调,然后我选择橙色。当我去其他一些画面+回来时,所选择的微调数据应该是橙色。但它再次从服务器到微调加载数据。任何人可以帮助我解决此问题?

我的code:

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    // ...

    如果(Constants.SPINNER!= NULL){
        spinner.setSelection(Constants.SPINNER);
    } 其他 {
        WebCall这里获取数据//
    }

    // ...

    spinner.setOnItemSelectedListener(新OnItemSelectedListener(){
        公共无效onItemSelected(适配器视图<>父,
            视图中查看,INT ARG2,长ARG3){
                Constants.SPINNER = spinner.getSelectedItemPosition();
 

类:

 公共静态整数SPINNER =;
 

解决方案 Android如何让蓝牙设备持续维持可见状态

您可以使用

  spinner.getSelectedItemPosition();
 

这会回报给你一个int,你可以保存

  prefEdit.putInt();
 

然后当你想要重新加载已保存你只需调用一切

  spinner.setSelection(prefs.getInt(钥匙,默认值));
 

或尝试像这样

保存:

  INT selectedPosition = yourSpinner.getSelectedItemPosition()
  editor.putInt(spinnerSelection,selectedPosition);
  editor.commit();
 

加载:

  yourSpinner.setSelection(prefs.getInt(spinnerSelection,0));
 

如果你是阵列使用它应该改变这样的

 字符串selectedString = yourArray [yourSpinner.getSelectedItemPosition()];
   editor.putString(spinnerSelection,selectedString);
   editor.commit();
 

检查数组[我]反对通过调用存储在prefs.if您使用ArrayList,而不是这部分可以在不循环完成值

  ArrayList.indexOf(prefs.getString(spinnerSelection,);
 

当你提交证明上述所有数组项了。显示没有人到数组。

I'm doing an Android app which can get data from a web service & load it into spinner. I need to maintain the selected data state of the spinner while I go to some screen & come back. For example, if I'm getting data from the web service as 1.apple 2.orange 3.grapes & loading it into the spinner, then I select orange. When I go to some other screen & come back, the selected spinner data should be orange. But it again loads data from the server into the spinner. Can anybody help me to resolve this?

My code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //...

    if (Constants.SPINNER != null ) {
        spinner.setSelection( Constants.SPINNER);
    } else {
        //WebCall here for getting data
    }

    //...

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent,
            View view, int arg2, long arg3) {
                Constants.SPINNER = spinner.getSelectedItemPosition();

In Constant class:

public static Integer SPINNER="";

解决方案

You can use

  spinner.getSelectedItemPosition(); 

that will return to you an int that you can save with

  prefEdit.putInt(); 

then when you want to re-load everything that has been saved you would just call

  spinner.setSelection(prefs.getInt("key", default));

OR TRY LIKE THIS

to Save:

  int selectedPosition = yourSpinner.getSelectedItemPosition()
  editor.putInt("spinnerSelection", selectedPosition);
  editor.commit();

to Load:

yourSpinner.setSelection(prefs.getInt("spinnerSelection",0));

if you are array used it should changed like this

 String selectedString = yourArray[yourSpinner.getSelectedItemPosition()];
   editor.putString("spinnerSelection", selectedString);
   editor.commit();

checking array[i] against the value stored in prefs.if you use an ArrayList instead this part could be done without the loop by calling

 ArrayList.indexOf(prefs.getString("spinnerSelection", "");

when you commit show all above array item gone. show no one into array.

 
精彩推荐
图片推荐