簡體   English   中英

如何使用共享首選項保存在微調器上和從選中的單選按鈕中選擇的數據

[英]How to use Shared Preferences to save data selected on Spinner and from checked Radio Button

我試圖將用戶從微調器中選擇的內容保存到共享的首選項對象中,並且還保存選中了哪個單選按鈕(正值或負值)。 然后顯示或從共享首選項對象檢索數據。

這是我的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

在下面,我寫了add()方法保存到共享首選項對象中:

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();
    }

在我的display()方法下面,用於從共享首選項對象檢索和顯示數據:

    // 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);
    }

}

您顯然誤解了SharedPreference類的工作方式。 由於您決定使用鍵“ specialTestName”保留一個int值,因此應按以下方式檢索它,以獲取保存的int:

int selectedPosition = sharedPrefObj.getInt("specialTestName", DEFAULTINT);

同樣,您從復選框中保存的是布爾值,因此您無法嘗試以String形式進行檢索。

boolean checkValue = sharedPrefObj.getBoolean("STValue", DEFAULTBOOLEAN);

順便說一句,我強烈建議您創建一個與SharedPreference直接交互的類,而不是直接在Activity / Fragment中使用它。

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM