我正在努力实现的目标
babyOneButton
GENERAL_PREFERENCES.xml
共享首选项文件中设置profile_selected属性babyOneButton
检查XML文件是否存在它实际上在做什么:
babyOneButton
NewChildProfile
提交后,该名称会显示在MainActivity
上,其中baby2的名字应该是什么?BABY_TWO_PROFILE
始终是NewBabyProfile
上显示的数据。MainActivity.java 公共类MainActivity扩展了Activity {
SharedPreferences generalPrefs;
SharedPreferences.Editor generalPrefsEditor;
public static String profileSelected;
public static String babyOneName;
public static String babyTwoName;
File file1, file2, file3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
file1 = new File("/data/data/com.parentingreminders/shared_prefs/BABY_ONE_PROFILE.XML");
file2 = new File("/data/data/com.parentingreminders/shared_prefs/BABY_TWO_PROFILE.XML");
String BABY_ONE_PROFILE = getString(R.string.baby_one_profile);
String BABY_TWO_PROFILE = getString(R.string.baby_two_profile);
SharedPreferences babyOneProfile = getSharedPreferences(BABY_ONE_PROFILE, 0);
SharedPreferences babyTwoProfile = getSharedPreferences(BABY_TWO_PROFILE, 0);
String babyOneName = babyOneProfile.getString("name", "name");
TextView babyOneNameOutput = (TextView) findViewById(R.id.baby_1_name);
babyOneNameOutput.setText(babyOneName.substring(0,1).toUpperCase() + babyOneName.substring(1));
String babyTwoName = babyTwoProfile.getString("name", "name");
TextView babyTwoNameOutput = (TextView) findViewById(R.id.baby_2_name);
babyTwoNameOutput.setText(babyTwoName.substring(0,1).toUpperCase() + babyTwoName.substring(1));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_baby_profile, menu);
return true;
}
public void babyOneButtonClick(View view) {
profileSelected = "1";
generalPrefs = getSharedPreferences(getString(R.string.general_preferences), Context.MODE_PRIVATE);
generalPrefsEditor = generalPrefs.edit();
generalPrefsEditor.putString("profile_selected", profileSelected).commit();
if (file1.exists()) {
Intent goToManageBaby1 = new Intent(this, ManageBaby1.class);
startActivity(goToManageBaby1);
} else {
Intent goToNewBabyProfile = new Intent(this, NewBabyProfile.class);
startActivity(goToNewBabyProfile);
}
}
public void babyTwoButtonClick(View view) {
profileSelected = "2";
generalPrefs = getSharedPreferences(getString(R.string.general_preferences), Context.MODE_PRIVATE);
generalPrefsEditor = generalPrefs.edit();
generalPrefsEditor.putString("profile_selected", profileSelected).commit();
if (file2.exists()) {
Intent goToManageBaby1 = new Intent(this, ManageBaby1.class);
startActivity(goToManageBaby1);
} else {
Intent goToNewBabyProfile = new Intent(this, NewBabyProfile.class);
startActivity(goToNewBabyProfile);
}
}}
NewBabyProfile.java 公共类NewBabyProfile扩展了Activity {
public static String gender = "na";
public static String name = "na";
public static String dobMonth = "January";
public static String dobDay = "01";
public static String dobYear = "1900";
public static String feedingOz = "00";
public static String feedingHrs = "00";
public static String awakeHrs = "00";
public static int activeStartHour = 0;
public static int activeStartMinute = 0;
public static int activeEnd = 0;
public static String allDay = "no";
public static Spinner mSpinner;
public static int profileNumber;
public static String profileCreated;
public static String profileSelected;
SharedPreferences babyProfile, generalPrefs;
SharedPreferences.Editor editor, generalPrefsEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_baby_profile);
mSpinner = (Spinner) findViewById(R.id.dob_month);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.months_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
mSpinner.setAdapter(adapter);
generalPrefs = getSharedPreferences(getString(R.string.general_preferences), Context.MODE_PRIVATE);
generalPrefsEditor = generalPrefs.edit();
// SharedPreferences initializations
profileSelected = generalPrefs.getString("profile_selected", "profileSelected");
if (profileSelected == "1") {
babyProfile = getSharedPreferences(getString(R.string.baby_one_profile), 0);
}
if (profileSelected == "2"){
babyProfile = getSharedPreferences(getString(R.string.baby_two_profile), 0);
}
editor = babyProfile.edit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_baby_profile, menu);
return true;
}
public void onRadioButtonClicked(View genderSelection){
boolean checked = ((RadioButton) genderSelection).isChecked();
switch(genderSelection.getId()) {
case R.id.gender_boy:
if (checked)
gender = "boy";
break;
case R.id.gender_girl:
if (checked)
gender = "girl";
break;
}
}
public void submitNewBabyProfile(View view) {
// Submit name
EditText nameInput = (EditText)findViewById(R.id.name_input);
name = nameInput.getText().toString().trim();
editor.putString("name",name).commit();
// Submit gender
editor.putString("gender",gender).commit();
// Submit date of birth
String dobMonth = mSpinner.getSelectedItem().toString();
editor.putString("dob_month",dobMonth).commit();
EditText dobDayInput = (EditText)findViewById(R.id.dob_day);
dobDay = dobDayInput.getText().toString().trim();
editor.putString("dob_day",dobDay).commit();
EditText dobYearInput = (EditText)findViewById(R.id.dob_year);
dobYear = dobYearInput.getText().toString().trim();
editor.putString("dob_year",dobYear).commit();
// Submit feeding information
EditText feedingOzInput = (EditText)findViewById(R.id.feeding_oz_input);
feedingOz = feedingOzInput.getText().toString().trim();
editor.putString("feeding_oz_input",feedingOz).commit();
EditText feedingHrInput = (EditText)findViewById(R.id.feeding_hr_input);
feedingHrs = feedingHrInput.getText().toString().trim();
editor.putString("feeding_hr_input",feedingHrs).commit();
// Submit nap information
EditText awakeInput = (EditText)findViewById(R.id.awake_input);
awakeHrs = awakeInput.getText().toString().trim();
editor.putString("awake_input",awakeHrs).commit();
// Submit notification active times
// Return to main activity
Intent goToMainActivity = new Intent(this, MainActivity.class);
startActivity(goToMainActivity);
}
}
答案 0 :(得分:0)
不确定是否存在问题,但您永远不应设置绝对路径字符串,因为您无法知道所有Android设备上的根数据路径
答案 1 :(得分:0)
SharedPreferences的文件名是内部的,这就是为什么有API。尝试替换
if (file2.exists())
用这个
if(babyTwoProfile.contains("name"))