【发布时间】:2014-01-15 13:36:51
【问题描述】:
在主要活动中,我有单选按钮,在方法 public void onRadioButtonClicked(View view) 中,我将每个选定的单选按钮与一个值相关联,所以我有两个变量 a 和 b,之后我单击按钮 bton i根据方法 ajouter,a 和 b 值进入数据库。直到现在一切正常,我决定使用 OnResume 和 OnPause 方法来保存活动状态,所以当我返回活动时,我看到单选按钮被选中,当我单击按钮保存信息时,问题就出现了,我得到 0 作为数据库中 a 和 b 的值,而不是我看到选择的值,奇怪的是,当我再次单击这些单选按钮时,然后在保存按钮,我得到正确的值!我的问题是如何在不再次单击所选单选按钮的情况下生成 a 和 b 的值?
public class ActivityUn extends Activity {
public void ajouter(View v) {
db.open();
db.insertMENAGE(a,b);
db.close();
Toast.makeText(getApplicationContext(), "Données Enregistrées",Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_un);
Button bton = (Button)findViewById(R.id.ajoutUn);
bton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ajouter(v); }
});
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.rm_13_1:
if (checked)
a=1;
break;
case R.id.rm_13_2:
if (checked)
a=2;
break;
case R.id.rm_14_1:
if (checked)
b=1;
break;
case R.id.rm_14_2:
if (checked)
b=2;
break;
case R.id.rm_14_3:
if (checked)
b=3;
findViewById
break;
case R.id.rm_14_4:
if (checked)
b=4;
break;
}
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
SharedPreferences.Editor editor = prefs3.edit();
editor.putBoolean("questionA", rm_13_1.isChecked());
editor.putBoolean("questionB", rm_13_2.isChecked());
editor.putBoolean("questionC", rm_14_1.isChecked());
editor.putBoolean("questionD", rm_14_2.isChecked());
editor.putBoolean("questionE", rm_14_3.isChecked());
editor.commit();
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences prefs3 = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
rm_13_1 = (RadioButton) findViewById(R.id.rm_13_1);
rm_13_2 = (RadioButton) findViewById(R.id.rm_13_2);
rm_14_1 = (RadioButton) findViewById(R.id.rm_14_1);
rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
rm_14_3 = (RadioButton) findViewById(R.id.rm_14_2);
Boolean rm_13_1A = false;
Boolean rm_13_2A = false;
Boolean rm_14_1A = false;
Boolean rm_14_2A = false;
Boolean rm_14_3A = false;
rm_13_1A = prefs3.getBoolean("questionA", false);
rm_13_2A = prefs3.getBoolean("questionB", false);
rm_14_1A = prefs3.getBoolean("questionC", false);
rm_14_2A = prefs3.getBoolean("questionD", false);
rm_14_3A = prefs3.getBoolean("questionE", false);
rm_13_1.setChecked(rm_13_1A);
rm_13_2.setChecked(rm_13_2A);
rm_14_1.setChecked(rm_14_1A);
rm_14_2.setChecked(rm_14_2A);
rm_14_3.setChecked(rm_14_3A);
}
【问题讨论】:
-
我做了很多努力都没有找到解决办法,请帮忙!
-
@Binghammar:有没有办法解决这个问题??
-
“值为空”是什么意思?
-
我确实将每个收音机与一个值相关联,然后我将值保存到数据库,问题是值不存在!!!
-
在视觉上,单选按钮被选中,但它所代表的值不存在,你知道为什么吗?
标签: android android-layout sharedpreferences