I am currently developing an app, which upon clearing data and/or installing must let the user enter data. So far I have achieved none of it, since every time I run it in emulator, it comes with the EditTexts
hidden and rest showing. I have tried an if-statement
but I am not sure if it is correct. Code is as follows:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkFirstRun();
savedData();
}
public void savedData() {
showGoalTextView = (TextView) findViewById(R.id.textView3);
showBeamsTextView = (TextView) findViewById(R.id.textView2);
showGoalEditText = (EditText) findViewById(R.id.editText2);
checkBtnPressed = (ImageButton) findViewById(R.id.imageButton5);
showBeamsEditText = (EditText) findViewById(R.id.editText4);
beamsGoal = (EditText) findViewById(R.id.editText4);
//Fetch the stored data and display it upon starting.
String goalSave = showGoalTextView.getText().toString();
String beamsSave = showBeamsTextView.getText().toString();
preferences = getSharedPreferences("userData", MODE_PRIVATE);
goalSave = preferences.getString("goals", goalSave);
if (goalSave != null) {
showGoalTextView.setText(goalSave);
hideAll();
} else {
resetAll();
}
beamsSave = preferences.getString("beam", beamsSave);
if (beamsSave != "0") {
showBeamsTextView.setText(beamsSave);
hideAll();
} else {
resetAll();
}
}
public void checkFirstRun() {
final String PREFS_NAME = "MyPrefsFile";
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
//Get current version code
int currentVersionCode = BuildConfig.VERSION_CODE;
//Get saved version
SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
int savedVersionCode = preferences.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
//Check for first run or upgrade
if (currentVersionCode == savedVersionCode) {
//No big deal
return;
} else if (savedVersionCode == DOESNT_EXIST) {
//TODO This is a new install
} else if (currentVersionCode > savedVersionCode) {
//TODO This is an upgrade
return;
}
//Update sharedPreferences with the current version code
preferences.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
public void hideAll() {
TextView showGoalTextView =
(TextView) findViewById(R.id.textView3);
EditText showGoalEditText =
(EditText) findViewById(R.id.editText2);
ImageButton checkBtnPressed =
(ImageButton) findViewById(R.id.imageButton5);
EditText showBeamsEditText =
(EditText) findViewById(R.id.editText4);
EditText beamsGoal =
(EditText) findViewById(R.id.editText4);
//Hide rest
showGoalEditText.setVisibility(View.GONE);
checkBtnPressed.setVisibility(View.GONE);
showBeamsEditText.setVisibility(View.GONE);
beamsGoal.setVisibility(View.GONE);
}
public void resetAll() {
//For when resetting
TextView showGoalTextView =
(TextView) findViewById(R.id.textView3);
EditText showGoalEditText =
(EditText) findViewById(R.id.editText2);
ImageButton checkBtnPressed =
(ImageButton) findViewById(R.id.imageButton5);
EditText showBeamsEditText =
(EditText) findViewById(R.id.editText4);
EditText beamsGoal =
(EditText) findViewById(R.id.editText4);
//Clear editTexts
showGoalEditText.getText().clear();
showBeamsEditText.getText().clear();
//Show all editTexts
showGoalEditText.setVisibility(View.VISIBLE);
checkBtnPressed.setVisibility(View.VISIBLE);
showBeamsEditText.setVisibility(View.VISIBLE);
beamsGoal.setVisibility(View.VISIBLE);
//Get the text view
TextView showResetTextView =
(TextView) findViewById(R.id.textView2);
//Get the value of the text view
String resetString = showResetTextView.getText().toString();
//Convert value to a number and reset it
Integer reset = Integer.parseInt(resetString);
reset = 0;
//Display the new value in the text view.
showResetTextView.setText(reset.toString());
showGoalTextView.setText("BEAMS");
}
Can anyone help me out with the correct if-statement
? I want it to show the user data to enter, when sharedPreferences are empty (which they will, if user installs app and/or clears data. If you would like to know what I exactly mean, please download my beta app: https://play.google.com/store/apps/details?id=jhpcoenen.connectlife.beams
Thank you in advance!
Aucun commentaire:
Enregistrer un commentaire