Java 如何使用共享首选项保存在微调器上和选中单选按钮上选择的数据

Java 如何使用共享首选项保存在微调器上和选中单选按钮上选择的数据,java,android,Java,Android,我试图将用户从微调器中选择的内容保存到共享首选项对象中,并将选中的单选按钮保存为正值或负值。然后显示或检索共享首选项对象中的数据 下面是我的onCreate方法的外观: public class ShoulderActivity extends Activity implements OnItemSelectedListener {
 
 
 // The attribute used to store the value of the special test id

我试图将用户从微调器中选择的内容保存到共享首选项对象中,并将选中的单选按钮保存为正值或负值。然后显示或检索共享首选项对象中的数据

下面是我的onCreate方法的外观:

public class ShoulderActivity extends Activity implements OnItemSelectedListener {
    
    
        
// The attribute used to store the value of the special test id <= used in sharedPreferences
       
        
    int selectedPosition;
    
        

// The attribute used to store the value (positive or negative) that correspond to the special test id <= used in sharedPreferences
        
    EditText STValue;
    
        
    TextView STNameTextView, STValueTextView;
    
        

public static final String DEFAULT = "N/A";
    

@Override
    
protected void onCreate(Bundle savedInstanceState) 
{
        
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.shoulder_layout);
    
        
    Spinner shoulderSpinner = (Spinner) findViewById(R.id.shoulder_spinner);
    
        

    //Create an ArrayAdapter using the string array and a default spinner
    
    ArrayAdapter<CharSequence> shoulderAdapter = ArrayAdapter
               .createFromResource(this, R.array.shoulder_tests,
                         android.R.layout.simple_spinner_item);
    
        

    //Specify the layout to use when the list of choices appears
       
    shoulderAdapter
           .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
        

    //Apply the adapter to the spinner
       
    shoulderSpinner.setAdapter(shoulderAdapter);
    
         
    shoulderSpinner.setOnItemSelectedListener(this);
    
    
    

}//END onCreate
下面是从共享首选项对象检索和显示数据的显示方法:

public void add(View view)
{
    
        
    //User SharedPreferences to save the value per special test
        
    SharedPreferences sharedPref = getSharedPreferences("STData", Context.MODE_PRIVATE);
    
        
    //to edit the data or add data inside my file "STData"
         
    SharedPreferences.Editor editor = sharedPref.edit();
    
        
    //Store the values in my file "STData"
        

    // ******** pull up the name of the test from ONITEM selected on SPINNER **********
    
       
    //editor.putString("specialTestName", STName.getText().toString() );
        

    int selectedPosition = shoulderSpinner.getSelectedItemPosition()
          
    editor.putInt("specialTestName", selectedPosition);
        

    editor.commit();
    
        

    //**************************************
        

    // get this value from the Radio Button *****************
    
        
    RadioButton posValuerb1 = (RadioButton) findViewById(R.id.positiveId);
    
        
    // Save the test value; if positive test radio button is checked means test is positive and vice-versa
        
    editor.putBoolean("STValue", posValuerb1.isChecked());
    
    
        
    //To confirm the changes or to save them
        
    editor.commit();
    }
    // Method to Display special tests performed in the STData file
    
public void display(View view)
{
    
    
        
    // User SharedPreferences to save the value per special name    
    SharedPreferences sharedPref = getSharedPreferences("STData", Context.MODE_PRIVATE);
    
        
    shoulderSpinner.setSelection(sharedPref.getInt("spinnerSelection",0));
        

    //save special test Value in String 'testValue' <= entered by the PT
        
    String testValue = sharedPref.getString("STValue", DEFAULT);
    
    
        

    // DISPlAY THE VALUES ON THE SCREEN OF ACTIVITY !!!!


        // Test Name
        
    STNameTextView = (TextView) findViewById(R.id.STView3);
         
    STNameTextView.setText(testName);
    
        // Name of the Special Test
         
    STValueTextView = (TextView) findViewById(R.id.STView4);
         
    STValueTextView.setText(testValue);
    }

}

你显然误解了SharedReference类的工作方式。 因为您决定使用键specialTestName保留一个int值 您应该按如下方式检索它,以获取保存的int:

int selectedPosition = sharedPrefObj.getInt("specialTestName", DEFAULTINT);
此外,您从复选框中保存的是一个布尔值,因此无法尝试以字符串形式检索

boolean checkValue = sharedPrefObj.getBoolean("STValue", DEFAULTBOOLEAN);
顺便说一句,我强烈建议您创建一个直接与SharedReference交互的类,而不是直接从Activity/Fragment使用它

希望这对你有帮助