So I am supposed to get user input for their name and number and add it to a list and display it to the ScrollView. However I don't know what else I am suppose to do in my last step to display it. I have a Main, Contact, and ContactsManager class.
Button contactButton;
EditText nameText;
EditText numberText;
ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactButton = findViewById(R.id.button);
nameText = findViewById(R.id.editTextName);
numberText = findViewById(R.id.editTextNumber);
scrollView = findViewById(R.id.scrollView);
final ContactsManager contactsManager = new ContactsManager();
contactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String enterText;
String enterNumber;
enterText = nameText.getText().toString();
enterNumber = numberText.getText().toString();
contactsManager.createContact(enterText, enterNumber);
scrollView.getDisplay();
}
});
}
public class Contact {
private String name;
private String phoneNumber;
public Contact(String nName, String NPhoneNumber) {
this.name = nName;
this.phoneNumber = NPhoneNumber;
}
public Contact() {
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
}
public class ContactsManager {
private ArrayList<Contact> contacts;
public Contact createContact(String name, String phoneNumber) {
Contact contact = new Contact();
this.contacts = new ArrayList<>();
return contact;
}
}
When I click the button to create contact nothing is displayed.
You are not adding the new contacts to your contact manager properly. Try something like:
public class ContactsManager {
private List<Contact> contacts;
public ContactsManager() {
contacts = new ArrayList<>(); // Initialise the list once.
}
public Contact createContact(String name, String phoneNumber) {
Contact contact = new Contact(name, phoneNumber);
contacts.add(contact); // Add the new contact to the list
return contact;
}
// Use this to access all your contacts
public List<Contact> getContacts() {
return contacts;
}
}
Once you have done this, you need to link your display with the list of contacts.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.