如何从 Android 中的 EditText 获取用户输入并将其保存到 ArrayList?
How to get user input from EditText in Android and save it to an ArrayList?
我是 Android 应用程序开发的新手,我正在为此苦苦挣扎...我已经尝试了下一个代码,但是当我执行它时它崩溃了,没有将数据保存在 arrayList 中...
我必须从编辑文本中存储联系人姓名、姓氏、电子邮件和 phone,并在填写字段后单击“添加联系人”按钮时将它们保存到 arrayList<Contact>
中。
顺便说一句!我正在使用 Sugar ORM 将数据存储在数据库中,这就是为什么最后一行
中的 newContact1.save();
public class AddContact extends Activity{
String contactName;
String contactSurname;
String contactEmail;
String contactPhone;
List<Contact> contacts = new ArrayList<Contact>();
/*
EditText txtContactName = (EditText) findViewById(R.id.txtContactName);
EditText txtContactSurname = (EditText) findViewById(R.id.txtContactSurname);
EditText txtContactEmail = (EditText) findViewById(R.id.txtContactEmail);
EditText phone = (EditText) findViewById(R.id.txtPhone);
Button btnAddContact = (Button) findViewById(R.id.btnAddContact);*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
setTitle("Add Contact");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_contact, 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);
}
public void getFirstName(View v){
EditText contactName = (EditText) findViewById(R.id.txtContactName);
contactName.getText().toString();
Log.d("AddContact","First Name: "+contactName);
}
public void getSurname(View v){
EditText contactSurname = (EditText) findViewById(R.id.txtContactSurname);
contactSurname.getText().toString();
Log.d("AddContact","Last Name: "+contactSurname);
}
public void getEmail(View v){
EditText contactEmail = (EditText) findViewById(R.id.txtContactEmail);
contactEmail.getText().toString();
Log.d("AddContact","mail: "+contactEmail);
}
public void getContactPhone(View v){
EditText contactPhone = (EditText) findViewById(R.id.txtContactPhone);
contactPhone.getText().toString();
Log.d("AddContact","Phone number: "+contactPhone);
}
public void saveNewContact(String contactName, String contactSurname, String contactPhone, String contactEmail){
Contact newContact1 = new Contact(contactName, contactSurname, contactPhone, contactEmail);
newContact1.save();
}
}
当EditText
中没有输入任何内容时,以下将导致崩溃:
contactName.getText().toString();
始终建议检查 null
:
if (contactName.getText() != null) {
contactName.getText().toString();
}
如何保存EditText
到ArrayList
的数据:
将从 EditText
提取的数据存储到 String
变量,然后写入 ArrayList
String contactNameStr = contactName.getText().toString();
String contactSurnameStr = contactSurname.getText().toString();
//Similarly do for others
Contact newContact1 = new Contact(contactNameStr, contactSurnameStr, contactPhone, contactEmail);
为按钮创建一个 onClickHanlder(这对于指示用户已完成输入名称很有用),以便您可以从 EditText 中获取文本,将其转换为字符串,然后将其添加到您的数组列表
试试这个:
contactName.add(myEditText.getText().toString().trim());
contactSurname.add(myEditText.getText().toString().trim());
希望这会有所帮助!
这是您的最终代码
public class AddContact extends Activity{
EditText txtContactName;
EditText txtContactSurname;
EditText txtContactEmail;
EditText phone;
Button btnAddContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
txtContactName = (EditText) findViewById(R.id.txtContactName);
txtContactSurname = (EditText) findViewById(R.id.txtContactSurname);
txtContactEmail = (EditText) findViewById(R.id.txtContactEmail);
phone = (EditText) findViewById(R.id.txtPhone);
btnAddContact = (Button) findViewById(R.id.btnAddContact);
btnAddContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addContact();
}
});
}
private void addContact() {
String name = txtContactName.getText().toString();
String sirName = txtContactSurname.getText().toString();
String email = txtContactEmail.getText().toString();
String phoneNumber = phone.getText().toString();
Contact newContact1 = new Contact(name, sirName, email, phoneNumber);
newContact1.save();
}
}
我用一种方法解决了这个问题!它工作正常,联系人保存在数组列表中:))
public void getData(View v){
EditText contactName = (EditText) findViewById(R.id.txtContactName);
String contactNameStr = contactName.getText().toString();
EditText contactSurname = (EditText) findViewById(R.id.txtContactSurname);
String contactSurnameStr= contactSurname.getText().toString();
EditText contactEmail = (EditText) findViewById(R.id.txtContactEmail);
String contactEmailStr = contactEmail.getText().toString();
EditText contactPhone = (EditText) findViewById(R.id.txtContactPhone);
String contactPhoneStr = contactPhone.getText().toString();
Contact newContact1 = new Contact(contactNameStr, contactSurnameStr, contactPhoneStr, contactEmailStr);
newContact1.save();
}
我是 Android 应用程序开发的新手,我正在为此苦苦挣扎...我已经尝试了下一个代码,但是当我执行它时它崩溃了,没有将数据保存在 arrayList 中...
我必须从编辑文本中存储联系人姓名、姓氏、电子邮件和 phone,并在填写字段后单击“添加联系人”按钮时将它们保存到 arrayList<Contact>
中。
顺便说一句!我正在使用 Sugar ORM 将数据存储在数据库中,这就是为什么最后一行
中的newContact1.save();
public class AddContact extends Activity{
String contactName;
String contactSurname;
String contactEmail;
String contactPhone;
List<Contact> contacts = new ArrayList<Contact>();
/*
EditText txtContactName = (EditText) findViewById(R.id.txtContactName);
EditText txtContactSurname = (EditText) findViewById(R.id.txtContactSurname);
EditText txtContactEmail = (EditText) findViewById(R.id.txtContactEmail);
EditText phone = (EditText) findViewById(R.id.txtPhone);
Button btnAddContact = (Button) findViewById(R.id.btnAddContact);*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
setTitle("Add Contact");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_contact, 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);
}
public void getFirstName(View v){
EditText contactName = (EditText) findViewById(R.id.txtContactName);
contactName.getText().toString();
Log.d("AddContact","First Name: "+contactName);
}
public void getSurname(View v){
EditText contactSurname = (EditText) findViewById(R.id.txtContactSurname);
contactSurname.getText().toString();
Log.d("AddContact","Last Name: "+contactSurname);
}
public void getEmail(View v){
EditText contactEmail = (EditText) findViewById(R.id.txtContactEmail);
contactEmail.getText().toString();
Log.d("AddContact","mail: "+contactEmail);
}
public void getContactPhone(View v){
EditText contactPhone = (EditText) findViewById(R.id.txtContactPhone);
contactPhone.getText().toString();
Log.d("AddContact","Phone number: "+contactPhone);
}
public void saveNewContact(String contactName, String contactSurname, String contactPhone, String contactEmail){
Contact newContact1 = new Contact(contactName, contactSurname, contactPhone, contactEmail);
newContact1.save();
}
}
当EditText
中没有输入任何内容时,以下将导致崩溃:
contactName.getText().toString();
始终建议检查 null
:
if (contactName.getText() != null) {
contactName.getText().toString();
}
如何保存EditText
到ArrayList
的数据:
将从 EditText
提取的数据存储到 String
变量,然后写入 ArrayList
String contactNameStr = contactName.getText().toString();
String contactSurnameStr = contactSurname.getText().toString();
//Similarly do for others
Contact newContact1 = new Contact(contactNameStr, contactSurnameStr, contactPhone, contactEmail);
为按钮创建一个 onClickHanlder(这对于指示用户已完成输入名称很有用),以便您可以从 EditText 中获取文本,将其转换为字符串,然后将其添加到您的数组列表
试试这个:
contactName.add(myEditText.getText().toString().trim());
contactSurname.add(myEditText.getText().toString().trim());
希望这会有所帮助!
这是您的最终代码
public class AddContact extends Activity{
EditText txtContactName;
EditText txtContactSurname;
EditText txtContactEmail;
EditText phone;
Button btnAddContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
txtContactName = (EditText) findViewById(R.id.txtContactName);
txtContactSurname = (EditText) findViewById(R.id.txtContactSurname);
txtContactEmail = (EditText) findViewById(R.id.txtContactEmail);
phone = (EditText) findViewById(R.id.txtPhone);
btnAddContact = (Button) findViewById(R.id.btnAddContact);
btnAddContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addContact();
}
});
}
private void addContact() {
String name = txtContactName.getText().toString();
String sirName = txtContactSurname.getText().toString();
String email = txtContactEmail.getText().toString();
String phoneNumber = phone.getText().toString();
Contact newContact1 = new Contact(name, sirName, email, phoneNumber);
newContact1.save();
}
}
我用一种方法解决了这个问题!它工作正常,联系人保存在数组列表中:))
public void getData(View v){
EditText contactName = (EditText) findViewById(R.id.txtContactName);
String contactNameStr = contactName.getText().toString();
EditText contactSurname = (EditText) findViewById(R.id.txtContactSurname);
String contactSurnameStr= contactSurname.getText().toString();
EditText contactEmail = (EditText) findViewById(R.id.txtContactEmail);
String contactEmailStr = contactEmail.getText().toString();
EditText contactPhone = (EditText) findViewById(R.id.txtContactPhone);
String contactPhoneStr = contactPhone.getText().toString();
Contact newContact1 = new Contact(contactNameStr, contactSurnameStr, contactPhoneStr, contactEmailStr);
newContact1.save();
}