如何让 Spinner 的默认值作为上次保存的值?Android

How to get Spinner a Default value as last saved value?Android

我已经给了这个在 android

中添加一个微调器

计划-

Spinner ctype = (Spinner) findViewById(R.id.s1);
String ct=ctype.getSelectedItem().toString();

所以在 Spinner Selection 中我给了这个数组

XML--

<Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/s1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:entries="@array/ct_array"
        android:prompt="@string/ct"
        android:layout_marginTop="2dp"
        android:layout_below="@+id/tt1"/>

字符串--

<string-array name="ct_array">
        <item>ftp://</item>
        <item>http://</item>
        <item>https://</item>
        <item>other</item>
    </string-array>

所以在选择它显示 ftp 作为默认值..所以我们可以根据方式更改它们..

所以这里我有一个设置页面,其中我已经给出了 1 Spinner,3 EditText 并且在共享首选项的帮助下我正在保存它们...

我最近为那些共享首选项添加了一个编辑页面...

map.get("xyz") 的帮助下,我正在阅读以前的 SharedValues.. 对于编辑文本,我已经给出了这样

 EditText value1= (EditText) findViewById(R.id.et1);
 value1.setText(map.get("value1"));

所以它显示以前保存的值 就像我想为 Spinner 添加一样,但它总是显示 FTP 任何人都可以建议我

我试过这个但是不行

   Spinner conntype = (Spinner) findViewById(R.id.s1);
   conntype.setSelection(map.get("conntype"));
                      // here it should be **(int)** not **(java.lang.stirng)**  

在这里,我首先尝试获取先前选择的值,如果先前的值为 http://,则显示 http:// 如果他想更改,他可以使用微调器更改,但默认值应该是用户之前选择的

有任何关于此类的建议

使用下面的代码:

 int spinnerPosition = getIndex(conntype, map.get("conntype"));
 conntype.setSelection(spinnerPosition);

此处,getIndex() 方法:

  private int getIndex(Spinner spinner, String value){

    for (int i=0;i<spinner.getCount();i++){
       if (spinner.getItemAtPosition(i).equals(value)){
         return i;
       }
    }
    return 0 ;
  }

希望对您有所帮助。