1.首先添加GreenDao的依赖。添加后整体如下:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'
}
}
greendao {
schemaVersion 1
daoPackage 'com.example.anonymous.greendao'
targetGenDir 'src/main/java'
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.mvptest"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile'org.greenrobot:greendao:3.0.1'
compile'org.greenrobot:greendao-generator:3.0.0'
testCompile 'junit:junit:4.12'
}
2.建立model
实体类:User
@Entity
public class User {
@Id
private String username;
private String password;
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Generated(hash = 2090636610)
public User(String username, String password) {
this.username = username;
this.password = password;
}
@Generated(hash = 586692638)
public User() {
}
}
业务类:
一个实现登录注册的接口和实现类:
public interface IUserBiz {
void login(String username,String password,OnLoginListener loginListener);
void register(String username,String password,OnRegisterListener onRegisterListener);
}
public class UserBiz implements IUserBiz {
@Override
public void login(String username, String password, OnLoginListener loginListener) {
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApplication.getmContext(),"warehouse",null);
DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
List userList = userDao.queryBuilder()
.build().list();
if(userList == null){
Log.e("register","数据库里无数据");
}else {
int flag = 0;
for(User user : userList){
if(user.getUsername().equals(username) && user.getPassword().equals(password)){
loginListener.loginSuccess();
flag = 1;
break;
}
}
if(flag == 0){
loginListener.loginFail();
}
}
}
@Override
public void register(String username, String password, OnRegisterListener onRegisterListener) {
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApplication.getmContext(),"warehouse",null);
DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
List userList = userDao.queryBuilder()
.build().list();
if(userList == null){
Log.e("register","数据库里无数据");
}else{
int flag = 0;
for(User user1 : userList){
if(user1.getUsername().equals(username)){
flag = 1;
onRegisterListener.registerFail();
break;
}
}
if (flag == 0) {
User user2 = new User(username,password);
userDao.insert(user2);
onRegisterListener.registerSuccess();
}
}
}
}
两个处理model业务类完成逻辑后通过Present返回数据给View的接口:
public interface OnRegisterListener {
void registerSuccess();
void registerFail();
}
public interface OnLoginListener {
void loginSuccess();
void loginFail();
}
View类:
public class MainActivity extends AppCompatActivity implements UserBaseActivity{
private UserPresent loginPresent = new UserPresent(this);
private EditText ma_username,ma_password;
private Button ma_login,ma_register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
ma_username = (EditText) findViewById(R.id.username);
ma_password = (EditText) findViewById(R.id.password);
ma_login = (Button) findViewById(R.id.login);
ma_register = (Button) findViewById(R.id.register);
ma_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register();
}
});
ma_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
}
@Override
public void login() {
loginPresent.login();
}
@Override
public void register() {
startActivity(new Intent(MainActivity.this,RegisterActivity.class));
}
public String getLoginUsername(){
return ma_username.getText().toString();
}
public String getLoginPassword(){
return ma_password.getText().toString();
}
}
public class RegisterActivity extends AppCompatActivity implements UserRegisterActivity {
private RegisterPresent registerPresent;
private Button ra_register,ra_back;
private EditText ra_username,ra_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
registerPresent = new RegisterPresent(this);
initView();
}
private void initView(){
ra_back = (Button) findViewById(R.id.rc_back);
ra_register = (Button) findViewById(R.id.ra_register);
ra_username = (EditText) findViewById(R.id.et_username);
ra_password = (EditText) findViewById(R.id.et_password);
ra_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
back();
}
});
ra_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
register();
}
});
}
@Override
public void register() {
Log.e("register","register");
registerPresent.register();
}
@Override
public String getUserName() {
return ra_username.getText().toString();
}
@Override
public String getPassword() {
return ra_password.getText().toString();
}
@Override
public void back() {
finish();
}
}
3.Present类:
public class RegisterPresent {
private UserBiz iUserBiz;
private RegisterActivity userRegisterActivity;
public RegisterPresent(RegisterActivity userRegisterActivity){
this.iUserBiz = new UserBiz();
this.userRegisterActivity = userRegisterActivity;
}
public void register(){
Log.e("registerPresent","正在注册0");
iUserBiz.register(userRegisterActivity.getUserName(),userRegisterActivity.getPassword(), new OnRegisterListener() {
@Override
public void registerSuccess() {
Toast.makeText(userRegisterActivity, "注册成功", Toast.LENGTH_SHORT).show();
}
@Override
public void registerFail() {
Toast.makeText(userRegisterActivity, "账号已经存在", Toast.LENGTH_SHORT).show();
}
});
}
}
public class UserPresent {
private UserBiz iUserBiz;
private MainActivity userBaseActivity;
public UserPresent(MainActivity userBaseActivity){
this.userBaseActivity = userBaseActivity;
iUserBiz = new UserBiz();
}
public void login(){
iUserBiz.login(userBaseActivity.getLoginUsername(), userBaseActivity.getLoginUsername(), new OnLoginListener() {
@Override
public void loginSuccess() {
userBaseActivity.startActivity(new Intent(userBaseActivity,DisplayActivity.class));
}
@Override
public void loginFail() {
Toast.makeText(userBaseActivity, "账号或者密码错误", Toast.LENGTH_SHORT).show();
}
});
}
}