因此,我应该让用户输入他们的姓名和号码,并将其添加到列表中并显示在ScrollView中。但是,我不知道在显示它的最后一步中还要做什么。我有一个Main,Contact和ContactsManager类。
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;
}
}
当我单击创建联系人的按钮时,没有任何显示。
答案 0 :(得分:0)
您没有将新联系人正确添加到联系人管理器中。尝试类似的东西:
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;
}
}
完成此操作后,您需要将显示内容与联系人列表相关联。