I develop an app for android.
I have a lot of variables of EditText type. I'd like to use a Button to save data which was entered by the user. The problem starts when I try to put every EditText one by one.
So I thought about using an ArrayList. I read that I can't put an ArrayList directly to SharedPreferences. But which way is better to do this? Use a Hash, serializable or one by one? I am a beginner so I don't know which way is better - it means more easy to use but longer.
In this case which I tried in one variable ("m" only) but when I click a Button to save this it goes out from app.
How can I improve this code to work correctly?
This is the code:
public class MainActivity extends ActionBarActivity {
public static final String MY_PREFS_NAME = "MyPrefsFile";
EditText m,m1,m2,m3,m00,m11,m22,m33;
Button button10;
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText m = (EditText) findViewById(R.id.message);
final EditText m1 = (EditText) findViewById(R.id.message1);
final EditText m2 = (EditText) findViewById(R.id.message2);
final EditText m3 = (EditText) findViewById(R.id.message3);
final EditText m00 = (EditText) findViewById(R.id.message00);
final EditText m11 = (EditText) findViewById(R.id.message11);
final EditText m22 = (EditText) findViewById(R.id.message22);
final EditText m33 = (EditText) findViewById(R.id.message33);
List<EditText> messageLIST = new ArrayList<EditText>(){{
add(m);
add(m1);
add(m2);
add(m3);
add(m00);
add(m11);
add(m22);
add(m33);
}};
for(int i = 0; i < messageLIST.size(); i++)
{
messageLIST.get(i);
}
sharedPreferences = getApplication().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String numberValue = sharedPreferences.getString("numberValue", null);
m.setText(numberValue);
}
#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_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/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);
}
public void saveButton(View view)
{
Intent intent = new Intent(this,MainActivity.class);
m.getText().toString();
SharedPreferences sharedPreferences = getApplication().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("numberValue", m.getText().toString());
editor.commit();
startActivity(intent);
}
}
I have no idea why you need to do this. You can just save comma seperated String for your all edit text values in preferenses.You dont need those lines ..
List<EditText> messageLIST = new ArrayList<EditText>(){{
add(m);
add(m1);
add(m2);
add(m3);
add(m00);
add(m11);
add(m22);
add(m33);
}};
for(int i = 0; i < messageLIST.size(); i++)
{
messageLIST.get(i);
}
Just do this
call getText.toString() on each edit text and append it by other value and then save it in a key to your preference
I would recommend you to create a Map<String, EditText>(). Add all your EditText's to the Map using put("Your key value", yourEditText). When you want to store it to SharedPreferences do this:
SharedPreferences sharedPreferences = getApplication().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
for(Map.Entry<String, EditText> entry : map.entrySet()){
editor.putString(entry.getKey(), entry.getValue().getText().toString();
}
editor.commit();
In the for loop, you can create a json string and save it to the sharedprefs like;
public void saveToSharedPrefs(List<EditText> messageLIST) {
JSONArray jsonArrayParent = new JSONArray();
for (int i = 0; i < messageLIST.size(); i++) {
String str = messageLIST.get(i).getText().toString().trim();
try {
JSONObject jsonChid = new JSONObject();
jsonChid.put("key", str);
jsonArrayParent.put(jsonChid.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
SharedPreferences sharedPreferences = getApplication().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("numberValue", jsonArrayParent.toString());
editor.commit();
}
and call this method in your saveButton method like
public void saveButton(View view) {
Intent intent = new Intent(this, MainActivity.class);
saveToSharedPrefs(messageLIST);
startActivity(intent);
}
Related
I am developing an application which has some switch buttons, which have SharedPreferences so that when the application is closed and reopened, the previously activated buttons remain so.
SharedPreferences in the MainActivity class:
private Spinner spipol;
private Switch quim, fil, fis, tri, esp, engl, inf, eti, reli, est, pol, dib, edf, mate;
private SharedPreferences sharedPreferences, sharfil, sharquim, shardfis, shartrig, sharesp, sharemater, sharing, sharinf, shareti, sharrel, sharest, sharpol, sharedf;
public static final String ex1 = "switch1";
public static final String ex = "switch";
dib = (Switch) findViewById(R.id.switch1);
sharedPreferences = getSharedPreferences("dib", MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPreferences.edit();
dib.setChecked(sharedPreferences.getBoolean(ex, false));
dib.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
editor.putBoolean(ex, true);
} else {
editor.putBoolean(ex, false);
}
editor.commit();
}
});
What I need is to know how I can do something similar to what I have with the switch buttons, but with my spinner:
spipol = (Spinner) findViewById (R.id.spipol);
String [] options = {"", "Short assignment", "Workshop", "Study", "Questions", "Review"};
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android.R.layout.simple_spinner_dropdown_item, options);
spipol.setAdapter (adapter);
}
I searched the internet for how to do it, but in all the places I saw, they used a save button, what I would like to know is if I can do this without the need to implement this button.
I tried this but it didn't work:
spipol.setOnItemSelectedListener (new AdapterView.OnItemSelectedListener () {
#Override
public void onItemSelected (AdapterView <?> parent, View view, int position, long id) {
CaptureSpinner = (String) parent.getItemAtPosition (position);
index = position;
System.out.println ("Index:" + index);
}
#Override
public void onNothingSelected (AdapterView <?> parent) {
}
});
}
public void savePreference (Context context, int index) {
SharedPreferences sharpref = getPreferences (getApplicationContext (). MODE_PRIVATE);
SharedPreferences.Editor editor = sharpref.edit ();
editor.putInt ("Data", index);
System.out.println ("Index:" + index);
editor.apply ();
}
}
First you need to change the savePreference() to the code below. you don't need to pass a Context or position. you can get the item selected directly from your Spinner to be able to call this method on onStop()
public void savePreference() {
SharedPreferences sharpref = getSharedPreferences("Spinner", MODE_PRIVATE);
SharedPreferences.Editor editor = sharpref.edit();
editor.putInt("Data", spipol.getSelectedItemPosition());
System.out.println("Index:" + spipol.getSelectedItemPosition());
editor.apply();
}
Call the mothod above in the onStope() method :
#Override
protected void onStop() {
super.onStop();
savePreference();
}
And on your onCreate() you just need to call this :
spipol = findViewById(R.id.spipol);
String[] options = {"", "Short assignment", "Workshop", "Study", "Questions", "Review"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, options);
spipol.setAdapter(adapter);
// here to restore the last selected item
SharedPreferences sharpref = getSharedPreferences("Spinner", MODE_PRIVATE);
spipol.setSelection(sharpref.getInt("Data", 0));
My code used to replace the previous entries and I realized I needed to use different keys for storing in shared preferences. Now my code does not output anything in the listview. please help
Java code where I ask for information about the person (name, favcolor, favfood)
public class personInfo extends AppCompatActivity {
EditText editText_name;
EditText editText_favfood;
EditText editText_favcolor;
Button button_save;
static int count = 0;
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_personinfo);
Log.d(MainActivity.class.getSimpleName(), "onCreate");
editText_name = (EditText) findViewById(R.id.editText_name);
editText_favcolor = (EditText) findViewById(R.id.editText_favcolor);
editText_favfood = (EditText) findViewById(R.id.editText_favfood);
button_save = (Button) findViewById(R.id.button_save);
button_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
SharedPreferences sharedPreferences = getSharedPreferences("ENTRIES", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name" + count, editText_name.getText().toString());
editor.putString("favcolor" + count, editText_favcolor.getText().toString());
editor.putString("favfood" + count, editText_favfood.getText().toString());
editor.apply();
editor.putInt("numOfEntries", count);
Intent it = new Intent(personInfo.this, listOfPeople.class);
startActivity(it);
}
});
}
}
Java code, page that is supposed to display the entries
public class listOfPeople extends AppCompatActivity {
ListView listView;
ArrayList<listEntry> list = new ArrayList<>();
listEntry le;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = (ListView) findViewById(R.id.listView_persons);
SharedPreferences sharedPreferences = getSharedPreferences("ENTRIES", MODE_PRIVATE);
int count = sharedPreferences.getInt("numOfEntries", 0);
for(int i = 1; i <= count; i++){
String nameValue = sharedPreferences.getString("name" + i, "");
String favcolorValue = sharedPreferences.getString("favcolor" + i, "");
String favfoodValue = sharedPreferences.getString("favfood" + i, "");
le = new listEntry(nameValue, favcolorValue, favfoodValue);
list.add(le);
}
personListAdapter adapter = new personListAdapter(this, R.layout.entryrow, list);
listView.setAdapter(adapter);
}
}
You are applying (saving) preferences before putting count
editor.apply();
editor.putInt("numOfEntries", count);
Just put before applying
editor.putInt("numOfEntries", count);
editor.apply();
call editor.apply() after editor.putInt("numOfEntries", count);
editor.putInt("numOfEntries", count);
editor.apply();
I'm trying to create a simple Android program that has text boxes for name, address, phone number, etc. When the user puts this information in and hits save it clears the text boxes, and when they hit the load button it retrieves the info. I know how to do it with one EditText box, but I can't figure out multiple. Can I do this inside one try/catch statement, or do I need more than one? This is what I have right now:
public class MainActivity extends ActionBarActivity {
private EditText textBoxName;
private EditText textBoxAddress;
private EditText textBoxCity;
private EditText textBoxPhone;
private EditText textBoxEmail;
private static final int READ_BLOCK_SIZE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBoxName = (EditText) findViewById(R.id.txtName);
textBoxAddress = (EditText) findViewById(R.id.txtAddress);
textBoxCity = (EditText) findViewById(R.id.txtCity);
textBoxPhone = (EditText) findViewById(R.id.txtPhone);
textBoxEmail = (EditText) findViewById(R.id.txtEmail);
Button saveBtn = (Button) findViewById(R.id.btnSave);
Button loadBtn = (Button) findViewById(R.id.btnLoad);
saveBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strName = textBoxName.getText().toString();
String strAddress = textBoxAddress.getText().toString();
String strCity = textBoxCity.getText().toString();
String strPhone = textBoxPhone.getText().toString();
String strEmail = textBoxEmail.getText().toString();
try {
FileOutputStream fOut = openFileOutput("textfile.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//write the string to the file
osw.write(strName);
osw.flush();
osw.close();
//display file saved messages
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
//clears the EditText
textBoxName.setText("");
textBoxAddress.setText("");
textBoxCity.setText("");
textBoxPhone.setText("");
textBoxEmail.setText("");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
});
loadBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try
{
FileInputStream fIn = openFileInput("textfile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = "";
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//convert the chars to a String
String readString = String.copyValueOf(inputBuffer, 0, charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
//set the EditText to the text that has been read
textBoxName.setText(s);
textBoxAddress.setText(s);
textBoxCity.setText(s);
textBoxPhone.setText(s);
textBoxEmail.setText(s);
Toast.makeText(getBaseContext(), "File loaded successfully!",
Toast.LENGTH_SHORT).show();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You can use Shared Preferences to store and retrieve information in Android.
you can use shared preferences for this purpose . Just put the value into shared preference and load when the user need this info like username and password saved locally into any login form.
I'm trying to hide the radio button and it's text if there is no value. The value for the radio button text, I'm getting it from the SQLite Database and I'm setting the visibility of radio button to invisible if one of those fields are empty. However, when I do so, even when there is content in one of the option, it just hides.
Below is my code.
FTR, I didn't hide option1, option2 and pollName because they are never null.
I'm sure I am doing something wrong with the if and else statements but I tried playing around but couldn't find the solution.
Full Code:
public class ViewPollActivity extends Activity {
DatabaseHandler DbHandler;
SharedPreferences settings;
SharedPreferences.Editor editor;
TextView txtPollId, txtPollName;
RadioButton txtOptionName1, txtOptionName2, txtOptionName3, txtOptionName4,
txtOptionName5;
Button btndisplay;
RadioGroup rdgrp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_poll);
txtPollId = (TextView) findViewById(R.id.pollId);
txtPollName = (TextView) findViewById(R.id.pollName);
txtOptionName1 = (RadioButton) findViewById(R.id.radio0);
txtOptionName2 = (RadioButton) findViewById(R.id.radio1);
txtOptionName3 = (RadioButton) findViewById(R.id.radio2);
txtOptionName4 = (RadioButton) findViewById(R.id.radio3);
txtOptionName5 = (RadioButton) findViewById(R.id.radio4);
btndisplay = (Button) findViewById(R.id.display);
rdgrp = (RadioGroup) findViewById(R.id.radioGroup1);
// Shared Preferences
// settings = getSharedPreferences("MYPREFS", 0);
// String username = settings.getString("username", null);
Intent i = getIntent();
String pollId = i.getStringExtra("pollId");
txtPollId.setText(pollId);
DbHandler = new DatabaseHandler(this);
Cursor c = DbHandler.getSinglePoll(pollId);
if (c.moveToFirst()) {
String PollName = c.getString(c.getColumnIndex("pollName"));
String optionName1 = c.getString(c.getColumnIndex("optionName1"));
String optionName2 = c.getString(c.getColumnIndex("optionName2"));
String optionName3 = c.getString(c.getColumnIndex("optionName3"));
String optionName4 = c.getString(c.getColumnIndex("optionName4"));
String optionName5 = c.getString(c.getColumnIndex("optionName5"));
txtPollName.setText(PollName);
txtOptionName1.setText(optionName1);
txtOptionName2.setText(optionName2);
txtOptionName3.setText(optionName3);
txtOptionName4.setText(optionName4);
txtOptionName5.setText(optionName5);
if(optionName3 == null) {
txtOptionName3.setVisibility(View.INVISIBLE);}
if (optionName4 == null) {
txtOptionName4.setVisibility(View.INVISIBLE);}
if(optionName5 == null) {
txtOptionName5.setVisibility(View.INVISIBLE);}
} else {
}
c.close();
DbHandler.close();
btndisplay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String selectedRadioValue = ((RadioButton) findViewById(rdgrp
.getCheckedRadioButtonId())).getText().toString();
Toast.makeText(ViewPollActivity.this, selectedRadioValue,
Toast.LENGTH_SHORT).show();
}
});
}// on Create
}// main class
Try to use isNull method to check null value.
if(OptionName3 ==null ){
txtOptionName3.setVisibility(View.GONE);
}
What I'm trying to make happen
User clicks babyOneButton
babyOneButton sets the profile_selected attribute in the GENERAL_PREFERENCES.xml shared preferences file
babyOneButton checks if the XML file exists
(1) If it does, send the user the to the page to edit the profile
(2) If it doesn't, send the user to the page to create a new profile
On either page, 'BABY_ONE_PROFILE.xml' data would be shown.
What it's actually doing:
User clicks babyOneButton
Sometimes after pressing submit on NewChildProfile, the name will show up on MainActivity where baby2's name should be?
No matter if the XML file exists or not, the user is always sent to the page to make a new profile. (If I switch the if/else statements around they'll always be sent to the manage page, so I'm assuming my way of finding if the profile exists isn't correct).
BABY_TWO_PROFILE is always the data shown on NewBabyProfile.
MainActivity.java
public class MainActivity extends 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
public class NewBabyProfile extends 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);
}
}
Not sure that's the problem but you should never set absolute path strings, as you cannot know the root data path on all Android devices
Look at this : Android basics data storage
The filenames for the SharedPreferences are internal, that's why there's an API. Try replacing
if (file2.exists())
with this
if(babyTwoProfile.contains("name"))