![](/img/trans.png)
[英]how to save spinner data and selected radio button saved to mysql database?
[英]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.