I'm new at Android app development and I'm struggling with this... I've tried the next code but it crashes when I execute it, is not saving the data in the arrayList...
I have to store contactName, surname, email and phone from the edittexts and save them into arrayList<Contact> when click the button AddContact after fill up the fields.
By the way! I'm using Sugar ORM to store data in the database, thats why the newContact1.save(); in the very last lines
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();
}
}