美好的一天,
我是android的新手。我想用SharedPreferences存储一些用户日期。我有一个活动 调查问卷 ,用户选择他的生日(使用名为wdullaer的datepicker自定义库),这里我有radiobutton组(知道用户是否结婚)如果用户在片段显示的关系中,建议选择伴侣生日。
当我进行活动时,它工作正常,我返回 调查问卷 活动,SharedPreferences恢复完美,并显示用户生日,radiobutton组位置,以及如果需要片段与伴侣生日。
问题是:当我关闭应用程序并重新打开时,活动中的数据将恢复,但片段(合作伙伴生日)中的数据将丢失。
抱歉我的英语不好,但我希望这个想法对你来说是可以理解的。
片段中的代码:
public class FragmentSettings extends Fragment implements DatePickerDialog.OnDateSetListener {
final static String PARTNER_YEAR = "partner_year";
final static String PARTNER_MONTH = "partner_month";
final static String PARTNER_DAY = "partner_day";
static int mYear;
static int mMonth;
static int mDay;
private TextView mDateTextView;
private SharedPreferences mSharedPreferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_settings, null);
mDateTextView = (TextView) v.findViewById(R.id.partner_date_view);
mDateTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
FragmentSettings.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setAccentColor(R.color.mdtp_accent_color);
dpd.setThemeDark(true);
dpd.show(getFragmentManager(), "DatePickerDialog");
}
});
return v;
}
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
String mDate = "Your partner birthday: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
mDateTextView.setText(mDate);
}
public void onDataSetDefault(int year, int monthOfYear, int dayOfMonth) {
String mDate = "Your partner birthday: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
mDateTextView.setText(mDate);
}
public void saveUserInfo() {
mSharedPreferences = getActivity().getPreferences(0);
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt(PARTNER_DAY, mDay);
editor.putInt(PARTNER_MONTH, mMonth);
editor.putInt(PARTNER_YEAR, mYear);
editor.commit();
Toast.makeText(getActivity(), "Saved Fragment", Toast.LENGTH_SHORT).show();
}
public void restoreUserInfo() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
mYear = mSharedPreferences.getInt(PARTNER_YEAR, mYear);
mMonth = mSharedPreferences.getInt(PARTNER_MONTH, mMonth);
mDay = mSharedPreferences.getInt(PARTNER_DAY, mDay);
if (mYear != 0 && mMonth != 0 && mDay != 0) {
onDataSetDefault(mYear, mMonth, mDay);
}
}
@Override
public void onResume() {
super.onResume();
restoreUserInfo();
}
@Override
public void onDestroyView() {
super.onDestroyView();
saveUserInfo();
}
}
问卷中的代码:
public class SettingsActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
final static String USER_YEAR = "user_year";
final static String USER_MONTH = "user_month";
final static String USER_DAY = "user_day";
final static String USER_STATUS = "user_status";
final static String USER_SEX = "user_sex";
//Need to store in SharedPreferences
static int mYear;
static int mMonth;
static int mDay;
int mSexId;
int mStatusId;
private TextView mDateTextView;
private RadioGroup mRadioSexGroup;
private RadioButton mMaleButton;
private RadioButton mFemaleButton;
private RadioGroup mRadioStatusGroup;
private RadioButton mInRelationshipButton;
private RadioButton mSingleButton;
private View mFragment;
private String mDate;
private Button mButton;
private SharedPreferences mSharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mDateTextView = (TextView) findViewById(R.id.date_view);
mButton = (Button) findViewById(R.id.save_button);
mFragment = findViewById(R.id.partner_container);
mFragment.setVisibility(View.GONE);
mRadioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
mMaleButton = (RadioButton) findViewById(R.id.male);
mFemaleButton = (RadioButton) findViewById(R.id.female);
mRadioStatusGroup = (RadioGroup) findViewById(R.id.user_status);
mInRelationshipButton = (RadioButton) findViewById(R.id.in_relationship);
mSingleButton = (RadioButton) findViewById(R.id.status_single);
mDateTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
SettingsActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setAccentColor(R.color.mdtp_accent_color);
dpd.setThemeDark(true);
dpd.show(getFragmentManager(), "DatePickerDialog");
}
});
mInRelationshipButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mFragment.setVisibility(View.VISIBLE);
} else {
mFragment.setVisibility(View.INVISIBLE);
}
}
});
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveUserInfo();
startActivity(new Intent(SettingsActivity.this, HomeActivity.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the HomeActivity/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
mDate = "Your Birthday: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
mDateTextView.setText(mDate);
}
public void onDataSetDefault(int year, int monthOfYear, int dayOfMonth) {
mDate = "Your Birthday: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
mDateTextView.setText(mDate);
}
public void saveUserInfo() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt(USER_DAY, mDay);
editor.putInt(USER_MONTH, mMonth);
editor.putInt(USER_YEAR, mYear);
mSexId = mRadioSexGroup.getCheckedRadioButtonId();
mStatusId = mRadioStatusGroup.getCheckedRadioButtonId();
editor.putInt(USER_SEX, mSexId);
editor.putInt(USER_STATUS, mStatusId);
editor.commit();
}
public void restoreUserInfo() {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
mYear = mSharedPreferences.getInt(USER_YEAR, mYear);
mMonth = mSharedPreferences.getInt(USER_MONTH, mMonth);
mDay = mSharedPreferences.getInt(USER_DAY, mDay);
if((mSharedPreferences.getInt(USER_SEX, mSexId)) == R.id.male){
mMaleButton.setChecked(true);
} else if ((mSharedPreferences.getInt(USER_SEX, mSexId)) == R.id.female){
mFemaleButton.setChecked(true);
} else {
mMaleButton.setChecked(false);
mFemaleButton.setChecked(false);
}
if(mSharedPreferences.getInt(USER_STATUS, mStatusId) == R.id.in_relationship){
mInRelationshipButton.setChecked(true);
}else if((mSharedPreferences.getInt(USER_STATUS, mStatusId)) == R.id.status_single){
mSingleButton.setChecked(true);
} else {
mInRelationshipButton.setChecked(false);
mSingleButton.setChecked(false);
}
if (mYear != 0 && mMonth != 0 && mDay != 0) {
onDataSetDefault(mYear, mMonth, mDay);
}
}
@Override
protected void onPause() {
super.onPause();
saveUserInfo();
}
@Override
protected void onResume() {
super.onResume();
restoreUserInfo();
}
}
无论如何,谢谢你!
答案 0 :(得分:3)
我的猜测是你保存到不同的SharedPreferences而不是从中检索。 使用以下代码保存:
mSharedPreferences = getActivity().getPreferences(0);
然后你用这个恢复:
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
尝试在两个地方使用第二个版本。
答案 1 :(得分:2)
使用
更改mSharedPreferences = getActivity().getPreferences(0);
mSharedPreferences=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
答案 2 :(得分:1)
您的数据未恢复,因为您正在尝试存储数据 在一个文件中,从另一个SharedPreferences文件中获取数据。
PreferenceManager.getDefaultSharedPreferences()
将使用默认的首选项文件名。这将使用package name
作为SharedPreferences file name
,
和
getActivity().getPreferences(0);
或getSharedPreferences (String name, int mode);
将使用given name
作为SharedPrefrence file name
。
此处getActivity().getPreferences(0);
只需将此活动的班级名称作为getSharedPreferences(String, int)
传入,即可调用基础preferences name
方法。
让它改变工作
mSharedPreferences = getActivity().getPreferences(0);
与
mSharedPreferences=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());