2014-05-23 59 views
0

我想顯示微調框的值在string而不是integer。現在它顯示爲'0','1'等整數。以字符串而不是整數顯示微調器的值?

制定活動:

public class Setting extends Activity { 
    bloodtype = (Spinner) findViewById(R.id.spinner2); 
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.blood_groups_arrays)); 
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    bloodtype.setAdapter(adapter2); 
    bloodtype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

     /** 
      * Called when a new item is selected (in the Spinner) 
      */ 
     public void onItemSelected(AdapterView<?> parent, 
      View view, int position, long id) { 
       sharedpreferences.edit().putString(BloodType, parent.getItemAtPosition(position).toString()).commit(); 
      } 

      public void onNothingSelected(AdapterView<?> parent) { 
      // Do nothing. 
      } 
    }); 

    if(sharedpreferences.contains(BloodType)) { 
     bloodtype.setSelection(sharedpreferences.getString(BloodType, "DEFAULT")); 
    } 
} 

中調用其主要活動值:

if(sharedpreferences.contains(Setting.BloodType)) {     
    show.setText(""+sharedpreferences.getString(Setting.BloodType, "DEFAULT")); 
} 

回答

1

爲了存儲陣列的價值,你填入你spinnerSharedPreferences使用下面的行中onItemSelected()方法:

sharedpreferences.edit().putString(BloodType, parent.getItemAtPosition(position).toString()).commit(); 

BloodType已成爲String典型值的e,因爲它在key-valueSharedPreferences中扮演key的角色。

0

從我的工作項目採取它應該工作:

adapterSpinner = new ArrayAdapter<String>(yourContenxt, 
       android.R.layout.simple_list_item_1, yourDataSource); 
     spinnerProfileType.setAdapter(adapterSpinner); 

正如你是 用一個字符串數組,你也能利用設置在XML微調項目:

機器人:項=「@陣列/ your_string_entry」

相關問題