文章目录
- 效果展示
 - 实现过程
 - 源码下载
 
效果展示

实现过程
因为要把 Person 类传到另一个界面,所以 Person 需要实现 Parcelable 接口
public class Person implements Parcelable {
    //字段声明 和 构造函数
    ......
    protected Person(Parcel in) {
        id = in.readInt();
        name = in.readString();
        age = in.readInt();
        phone = in.readString();
        email = in.readString();
    }
    public static final Creator<Person> CREATOR = new Creator<Person>() {
        
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }
        
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
    //setter 和 getter 方法
    ......
    
    
    public int describeContents() {
        return 0;
    }
    
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
        dest.writeInt(age);
        dest.writeString(phone);
        dest.writeString(email);
    }
}MainActivity 中点击编辑时,打开新 Activity,并且传递数据
    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:
                //弹出确认对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                AlertDialog dialog = builder.setTitle("警告")
                        .setMessage("删除操作不可恢复,确定删除吗?")
                        .setPositiveButton("确定", this)
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();
                break;
        }
        return super.onContextItemSelected(item);
    }IDao中声明新的编辑方法
public interface IDao<T> {
    ......
    int update(T t);
}PersonDao 中重写 update 方法
    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
public class ContactFormActivity extends AppCompatActivity implements View.OnClickListener {
    ......
    private int editID;
    private int mode;
    private static final int MODE_ADD = 0;
    private static final int MODE_EDIT = 1;
    
    protected void onCreate(Bundle savedInstanceState) {
        ......
        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"));
        }
    }
    ......
    
    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;
            ......
        }
    }
}源码下载









