2013-10-17 41 views
0

我试图使发生更改页值

  • 用户点击babyOneButton
  • babyOneButton设置在GENERAL_PREFERENCES.xml共享偏好的profile_selected属性文件
  • babyOneButton检查XML文件存在
  • (1)如果是,则发送用户到页面以编辑配置文件
  • (2)如果不存在,则将用户发送到pa ge创建新配置文件
  • 在任一页上,都会显示'BABY_ONE_PROFILE.xml'数据。

什么它实际上是这样做的:

  • 用户点击babyOneButton
  • 有时按提交NewChildProfile后,名称将显示在MainActivity了哪里宝宝2名应该是什么?
  • 无论XML文件是否存在,用户总是被发送到页面以创建一个新的配置文件。 (如果我切换if/else语句,他们总是会被发送到管理页面,所以我假设我找到配置文件存在的方式是不正确的)。
  • BABY_TWO_PROFILE总是显示在NewBabyProfile上的数据。

MainActivity.java 公共类MainActivity延伸活动{

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 PU BLIC类NewBabyProfile延伸活动{

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

不知道这是问题,但你永远不应该设置绝对路径字符串,你可以不知道在所有的Android设备

看看根数据路径这个:Android basics data storage

+0

呀更换

if (file2.exists()) 

,我想这是不好的。我也得到了来自Eclipse的警告:“不要硬编码”/ data /“;使用Context.getFilesDir()。getPath() 而不是” 任何想法如何把它放在我的代码? BABY_ONE-PROFILE.xml会进入getPath()吗? – tuzion

0

SharedPreferences的文件名是内部的,这就是为什么有一个API。尝试用这种

if(babyTwoProfile.contains("name")) 
+0

这似乎已修复该部分!谢谢! :) – tuzion