如何从Android中的EditText获取用户输入并将其保存到ArrayList?

时间:2015-03-24 19:44:14

标签: android arraylist android-edittext

我是Android应用程序开发的新手,我正在努力解决这个问题...我已经尝试了下一个代码,但是当我执行它时它崩溃了,而不是将数据保存在arrayList中......

我必须在edittexts中存储contactName,姓氏,电子邮件和电话,并在填写完字段后单击AddContact按钮将其保存到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();
    }
}

4 个答案:

答案 0 :(得分:1)

如果在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);

答案 1 :(得分:1)

为按钮创建一个onClickHanlder(这对于指示用户输入名称很有用),以便您可以从EditText获取文本,将其转换为String,并将其添加到ArrayList < / p>

试试这个:

contactName.add(myEditText.getText().toString().trim());

contactSurname.add(myEditText.getText().toString().trim());

希望这会有所帮助!

答案 2 :(得分:1)

这是你的最终代码

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();


  }
}

答案 3 :(得分:0)

我只是用一种方法做了这个!它工作正常,联系人保存在arraylist中:))

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();

}