I’m trying to display text that is input by the user if it is not null but I’m having some trouble (I’m a Java noob). Here is the flow of my app:
Starts at Main.java with a button:
-Button: Profile
If you click profile, the app goes to the profile page Display.java that also has a button:
-Button: Edit Profile
-This view also displays the information (name, phone number, zip code, etc)
When a user clicks Edit Profile, the program goes to a form at EditProfile.java, which has a form where users enter the information and then there is a button to submit.
-Button: Submit
This submit button takes the user back to the previous view (Display.java) and displays the information that was previously entered in the form with the string resultText.
I’m not sure how to make this work. If anyone has any suggestions, I’d really appreciate the help!
Edit: One thing to note is that I’m getting a “Dead Code” error on the if expression in Display.java
Display.java:
public class Display extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
String newline = System.getProperty("line.separator");
TextView resultText = (TextView) findViewById(R.id.resultText);
Bundle bundle = getIntent().getExtras();
String firstName = null;
String lastName;
String phoneNumber;
String city;
String zipCode;
if(firstName != null) {
firstName = bundle.getString("EditTextFirstName");
lastName = bundle.getString("EditTextLastName");
phoneNumber = bundle.getString("EditTextPhoneNumber");
city = bundle.getString("EditTextCity");
zipCode = bundle.getString("EditTextZipCode");
resultText.setText("Name: " + firstName + " " + lastName + newline + "Phone Number: " + phoneNumber +
newline + "City: " + city + newline + "Zip Code: " + zipCode + newline);
}
Button profile = (Button) findViewById(R.id.button1);
profile.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Display.this, EditProfile.class));
}
});
}
}
EditProfile.java:
public class EditProfile extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
}
public void sendFeedback(View button) {
final EditText firstnameField = (EditText)this.findViewById(R.id.EditTextFirstName);
String firstname = firstnameField.getText().toString();
final EditText lastnameField = (EditText) findViewById(R.id.EditTextLastName);
String lastname = lastnameField.getText().toString();
final EditText phoneNumberField = (EditText) findViewById(R.id.EditTextPhoneNumber);
String phoneNumber = phoneNumberField.getText().toString();
final EditText cityField = (EditText) findViewById(R.id.EditTextCity);
String city = cityField.getText().toString();
final EditText zipCodeField = (EditText) findViewById(R.id.EditTextZipCode);
String zipcode = zipCodeField.getText().toString();
int count = 0;
int fnlen=firstname.length();
int lnlen=lastname.length();
int phlen=phoneNumber.length();
int citylen=city.length();
int zclen=zipcode.length();
if (fnlen<=0){
firstnameField.setError("Enter your first name");
}
else {
count += 1;
}
if (lnlen<=0){
lastnameField.setError("Enter your last name");
}
else {
count += 1;
}
if (phlen<=0){
phoneNumberField.setError("Enter your ten digit phone number");
}
else if (phlen!=10){
phoneNumberField.setError("Phone number must be ten digits");
}
else {
count += 1;
}
if (citylen<=0){
cityField.setError("Enter your city");
}
else {
count += 1;
}
if (zclen<=0){
zipCodeField.setError("Enter your Zip Code");
}
else if (zclen!=5){
zipCodeField.setError("Enter a five digit zip code");
}
else {
count += 1;
}
if (count == 5) {
Intent intent = new Intent();
intent.setClass(this,Display.class);
intent.putExtra("EditTextFirstName",firstnameField.getText().toString());
intent.putExtra("EditTextLastName",lastnameField.getText().toString());
intent.putExtra("EditTextPhoneNumber",phoneNumberField.getText().toString());
intent.putExtra("EditTextCity",cityField.getText().toString());
intent.putExtra("EditTextZipCode",zipCodeField.getText().toString());
startActivity(intent);
}
else {
count = 0;
}
}
}
try with `Intent i= getIntent();
if(i.getExtras()!=null){