我正在学校项目中,我必须编写一个MVP架构模式的Android项目。我也开始对Google Dagger 2进行一些研究。现在我只是试图检查一个用户是否存在于数据库中。众所周知,问题在于通过上下文。我寻找一个简洁的答案,但我找不到任何东西。我试图这样做。Android MVP +谷歌匕首2 + SQLite
代码使用匕首2方面:
public class DaggerApplication extends Application {
DaggerComponent daggerComponent;
@Override
public void onCreate() {
super.onCreate();
daggerComponent = DaggerComponent.builder().daggerModule(new DaggerModule(this)).build();
daggerComponent.inject(this);
}
public DaggerComponent getAppComponent(){
return daggerComponent;
}
}
@Module
public class DaggerModule {
private final DaggerApplication daggerApplication;
public DaggerModule(DaggerApplication daggerApplication){
this.daggerApplication = daggerApplication;
}
@Provides
@Singleton
Context providesApplicationContext() {
return this.daggerApplication;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Context context) {
return context.getSharedPreferences("My_Pref", Context.MODE_PRIVATE);
}
}
@Singleton
@Component
(modules = {DaggerModule.class})
public interface DaggerComponent {
void inject(DaggerApplication daggerApplication);
void inject(SigninActivityFragment signinActivityFragment);
}
然后我让我的背景在这样的片段:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((DaggerApplication)getActivity().getApplication()).getAppComponent().inject(this);
accountPresenter = new AccountPresenter(this,new MyDatabaseSource(context));
}
的MyDatabaseSource需要这方面的数据库工作。然后,这个MyDatabaseSource打开数据库,从数据库中获取用户并关闭它。像这样:
public class MyDatabaseSource implements MyModel {
public MyDatabaseSource(@NonNull Context context) {
checkNotNull(context);
myLocalDatabaseOpenHelper = new MyLocalDatabaseOpenHelper(context);
}
//Implementing MyModel functions here
}
这就是为什么我需要上下文。
public class MyLocalDatabaseOpenHelper extends SQLiteOpenHelper {
private static final String MY_LOCAL_DATABASE_NAME = "MyUserDatabase";
private static final int MY_LOCAL_DATABASE_VERSION = 1;
public MyLocalDatabaseOpenHelper(Context context) {
super(context, MY_LOCAL_DATABASE_NAME, null, MY_LOCAL_DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//Created my database here
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Not required as at version 1
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Not required as at version 1
}
}
我创建了MyDatabaseSource实现的接口,我称它为MyModel。
下面是代码:
public interface MyModel {
void insertUser(User user);
void deleteUser(int index);
void updateUser(User user);
ArrayList <User> getUsers();
User getUser(int index);
}
然后我的演讲有它在,才能成为我的视图和模型之间的中间人所需要的一切。
这是我的主持人代码:
public AccountPresenter(AccountView accountView, MyModel myModel) {
this.accountView = accountView;
this.myModel = myModel;
}
public void onSignInButtonClicked() {
String username = accountView.getUsername();
String password = accountView.getPassword();
if (username.isEmpty()) {
accountView.setUsernameErrorMessage(R.string.activity_signin_username_error);
}
if (password.isEmpty()) {
accountView.setPasswordErrorMessage(R.string.activity_signin_password_error);
}
ArrayList<User> userArrayList = myModel.getUsers();
if (userArrayList.size() != 0) {
for (int i = 0; i < userArrayList.size(); i++) {
if (username.equals(userArrayList.get(i).getUserName())) {
if (password.equals(userArrayList.get(i).getUserPassword())) {
accountView.showUserExists(R.string.activity_signin_user_exists_toast);
} else {
accountView.showIncorrectPassword(R.string.activity_signin_incorrect_password_toast);
}
} else {
accountView.showUserDoesNotExist(R.string.activity_signin_user_does_not_exist_toast);
}
}
} else {
accountView.showNoUserExists(R.string.activity_signin_no_exists_toast);
}
}
我想知道这是否是使用谷歌匕首2.我个人觉得应该是实现这个另一种方式实现MVP的正确方法;将主题背景传递给主持人会胜过使用Google Dagger的目的。我刚开始研究Google Dagger 2和MVP模式。所以任何建议都会有帮助。我只是希望它是正确的。不想放松评分。 :)
任何建议和意见将有所帮助。 :)
你可能会发现这篇文章很有用[为什么Android中的活动不是UI元素](http://www.techyourchance.com/activities-android/) – tchelidze