我正在使用Dao SQLite验证来自用户的电子邮件。发送给Dao的电子邮件是成功的,但是当我尝试从Dao的返回中获取数据时,返回结果为NULL。
这是Dao界面:
@Dao
public interface NoteDao {
@Query("SELECT * FROM user_table WHERE email = :email")
Note get_UserInfo(String email);
}
这是存储库:
public class NoteRepository {
private NoteDao noteDao;
private LiveData<List<Note>> allNotes;
public NoteRepository(Application application) {
NoteDatabase database = NoteDatabase.getDatabase(application);
noteDao = database.noteDao();
allNotes = noteDao.getAllNotes();
}
public Note get_UserInfo(String email){
return noteDao.get_UserInfo(email);
}
}
这是ViewModel类:
public class NoteViewModel extends AndroidViewModel {
private NoteRepository repository;
private LiveData<List<Note>> allNotes;
public NoteViewModel(@NonNull Application application) {
super(application);
repository = new NoteRepository(application);
allNotes = repository.getAllNotes();
}
public Note get_UserInfo(String email) {
return repository.get_UserInfo(email);
}
}
这是寄存器类:
public class Register extends Fragment{
private NoteViewModel noteViewModel;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
Note userInfo = noteViewModel.get_UserInfo(email);
String user_email = userInfo.getEmail();
if (user_email != null){
Note note = new Note();
note.setEmail(email);
note.setUsername(username);
note.setPassword(new_password);
noteViewModel.insert(note);
Toasty.success(getActivity(), "Register Success", Toasty.LENGTH_SHORT).show();
UserAccount.fragmentManager.beginTransaction().replace(R.id.user_fragment, new Login()).commit();
}else {
Toasty.error(getActivity(), "Please enter a valid Email", Toasty.LENGTH_SHORT).show();
}
}
}