public void onClick(View view) { String name = edtName.getText().toString(); if (name.equals("")) { Toast.makeText(Contacs.this, "姓名不能为空", Toast.LENGTH_LONG).show(); return; }
这i先获得控件上的值,然后,将其存放到User这个实体类 public class User {
public int _id; public int image_id; public String name; public String mobilePhone; public String officePhone; public String familyPhone; public String position; public String company; public String address; public String zipCode; public String email; public String otherContact; public String remark;
发布评论
评论(1)
直接在保存按钮上设置监听器
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = edtName.getText().toString();
if (name.equals("")) {
Toast.makeText(Contacs.this, "姓名不能为空", Toast.LENGTH_LONG).show();
return;
}
这i先获得控件上的值,然后,将其存放到User这个实体类
public class User {
public int _id;
public int image_id;
public String name;
public String mobilePhone;
public String officePhone;
public String familyPhone;
public String position;
public String company;
public String address;
public String zipCode;
public String email;
public String otherContact;
public String remark;
}
添加联系人代码。
String mobilePhone = edtMobilePhone.getText().toString();
String officePhone = edtOfficePhone.getText().toString();
String familyPhone = edtFamilyPhone.getText().toString();
String position = edtPosition.getText().toString();
String company = edtCompany.getText().toString();
String address = edtAddress.getText().toString();
String email = edtEmail.getText().toString();
String zipCode = edtZipCode.getText().toString();
String otherContact = edtOtherContact.getText().toString();
String remark = edtRemark.getText().toString();
int imageId = images[imagePosition % images.length];
User user = new User();
//得到user的对象后,就可以将其存入
user.address = address;
user.company = company;
user.email = email;
user.familyPhone = familyPhone;
user.image_id = imageId;
user.mobilePhone = mobilePhone;
user.officePhone = officePhone;
user.name = name;
user.otherContact = otherContact;
user.position = position;
user.remark = remark;
user.zipCode = zipCode;
long success = DBHelper.getDbHelper(Contacs.this).save(user);
if (success != -1) {
Toast.makeText(Contacs.this, "保存成功!", Toast.LENGTH_LONG).show();
// resultCode就是为1的时候,添加用户成功,返回到主界面
setResult(1);
//应该跳转但是当前页面还在。所以finish()掉就可以了
finish();
} else {
Toast.makeText(Contacs.this, "保存失败!请重型操作", Toast.LENGTH_LONG).show();
//resultCode为0代表添加失败。这个setResult时候,会回到mainActivity。并且条用OnActivityResult()
//在这个方法在判断这个resultCode。并执行相应的操作,比如刷新界面
setResult(0);
finish();
}
}
});
btnReturn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//把其当做activity返回处理。那么将不会刷新
setResult(0);
finish();
}
});
}
主界面里的接受跳转的方法,就是怎么处理的这个保存值得。重写一下这个方法。并去判断它的requestCode,然后做出相应的操作
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == 1) {
//添加后保存了 ,需要将现有的数据库刷新 刷新用户列表listView,里面要使用到数据库的查询操作。所以这一部分也将下一节中讲解。
loadUserList();
} else if (resultCode == 0) {
//不进行刷新
}
}
if(requestCode==1){
if(resultCode==1){
loadUserList();
}
else {
}
}
super.onActivityResult(requestCode, resultCode, data);
}