1. 明确需求,界面如下:可存,可根据id读取数据。
public class UserBean { private String mFirstName; private String mLastName; public UserBean(String firstName, String lastName) { this. mFirstName = firstName; this. mLastName = lastName; } public String getFirstName() { return mFirstName; } public String getLastName() { return mLastName; } }
public interface IUserModel { void setID(int id); void setFirstName(String firstName); void setLastName(String lastName); int getID(); UserBean load(int id);// 通过id读取user信息,返回一个UserBean }
public interface IUserView { int getID(); String getFristName(); String getLastName(); void setFirstName(String firstName); void setLastName(String lastName); }
public class UserPresenter { private IUserView mUserView; private IUserModel mUserModel; public UserPresenter(IUserView view) { mUserView = view; mUserModel = new UserModel(); } public void saveUser( int id, String firstName, String lastName) { mUserModel.setID(id); mUserModel.setFirstName(firstName); mUserModel.setLastName(lastName); } public void loadUser( int id) { UserBean user = mUserModel.load(id); mUserView.setFirstName(user.getFirstName()); // 通过调用IUserView的方法来更新显示 mUserView.setLastName(user.getLastName()); } }
public class MainActivity extends Activity implements OnClickListener,IUserView { UserPresenter presenter; EditText id,first,last; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main); findViewById(R.id. save).setOnClickListener( this); findViewById(R.id. load).setOnClickListener( this); id = (EditText) findViewById(R.id. id); first = (EditText) findViewById(R.id. first); last = (EditText) findViewById(R.id. last); presenter = new UserPresenter( this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id. save: presenter.saveUser(getID(), getFristName(), getLastName()); break; case R.id. load: presenter.loadUser(getID()); break; default: break; } } @Override public int getID() { return new Integer( id.getText().toString()); } @Override public String getFristName() { return first.getText().toString(); } @Override public String getLastName() { return last.getText().toString(); } @Override public void setFirstName(String firstName) { first.setText(firstName); } @Override public void setLastName(String lastName) { last.setText(lastName); } }
8. 源码地址
http://download.csdn.net/detail/knxw0001/7983807