Person实现Parcelable接口
public class Person implements Parcelable {
private int id;
private String name;
private int age;
private String phone;
private String email;
......
@Override
public void writeToParcel(Parcel dest,int flags) {
dest.writeInt(this.id);
dest.writeString(this.name);
dest.writeInt(this.age);
dest.writeString(this.phone);
dest.writeString(this.email);
}
protected Person(Parcel in) {
this.id = in.readInt();
this.name = in.readString();
this.age = in.readInt();
this.phone = in.readString();
this.email = in.readString();
}
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
return new Person(source);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
}
MainActivity中点击编辑时,打开新Activity,并且传递数据
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case MENU_ITEM_EDIT:
//打开新的Activity
Intent intent = new Intent(this,ContactFormActivity.class);
intent.putExtra("person",persons.get(actionPosition));
startActivity(intent);
break;
case MENU_ITEM_DELETE:
......
break;
}
return super.onContextItemSelected(item);
}
ContactFormActivity
private int mode;
private static final int MODE_ADD = 0;
private static final int MODE_EDIT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_form);
initViews();
setListeners();
Person person = getIntent().getParcelableExtra("person");
//判断是否接收到数据
if(person == null){
//增加状态
mode = MODE_ADD;
}else {
mode = MODE_EDIT;
etUsername.setText(person.getName());
etAge.setText(person.getAge()+"");
etPhone.setText(person.getPhone());
etEmail.setText(person.getEmail());
btnSave.setText("编辑");
etUsername.setEnabled(false);
etUsername.setTextColor(Color.parseColor("#999999"));
}
}
IDao
public interface IDao<T> {
......
int update(T t);
}
PersonDao中重写update方法
@Override
public int update(Person person) {
DBOpenHelper dbOpenHelper = new DBOpenHelper(context);
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
//执行业务,修改数据
String table = "users";
ContentValues values = new ContentValues();
values.put("_age",person.getAge());
values.put("_phone",person.getPhone());
values.put("_email",person.getEmail());
String whereClause = " _id = ?";
String[] whereArgs = { person.getId()+"" };
int affectedRows = db.update(table,values,whereClause,whereArgs);
db.close();
db = null;
return affectedRows;
}
ContactFormActivity
private int editID;
private int mode;
private static final int MODE_ADD = 0;
private static final int MODE_EDIT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_form);
initViews();
setListeners();
Person person = getIntent().getParcelableExtra("person");
//判断是否接收到数据
if(person == null){
//增加状态
mode = MODE_ADD;
}else {
mode = MODE_EDIT;
editID = person.getId();
etUsername.setText(person.getName());
etAge.setText(person.getAge()+"");
etPhone.setText(person.getPhone());
etEmail.setText(person.getEmail());
btnSave.setText("编辑");
etUsername.setEnabled(false);
etUsername.setTextColor(Color.parseColor("#999999"));
}
}
......
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_save:
//获取表单中的数据
String name = etUsername.getText().toString().trim();
String phone = etPhone.getText().toString().trim();
String email = etEmail.getText().toString().trim();
int age = Integer.parseInt(etAge.getText().toString());
Person person = new Person(name,age,phone,email);
PersonDao personDao = new PersonDao(this);
if(mode == MODE_ADD){
long id = personDao.insert(person);
if(id == -1){
Toast.makeText(this,"姓名或电子邮件可能冲突!",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"数据增加成功",Toast.LENGTH_SHORT).show();
etUsername.setText(null);
etPhone.setText(null);
etEmail.setText(null);
etAge.setText(null);
}
}else {
person.setId(editID);
int affectedRows = personDao.update(person);
if(affectedRows > 0){
Toast.makeText(this,"修改数据成功",Toast.LENGTH_SHORT).show();
finish();
}else {
Toast.makeText(this,"修改数据失败",Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.btn_back:
finish();
break;
}
}